PYTHON INTERMEDIATE’S GUIDE | WORKING WITH FILES – PART I

AN INTRODUCTION TO WORKING WITH FILES

HEYYYYYY, how is it going? I welcome you to the first intermediate Python lesson. If you are a total beginner in Python/Programming you may not get any of this stuff. So, please check out my older articles and master the fundamentals of Python programming before going any further.

Today we will learn how to control files using Python. This includes reading files, writing to them or just opening them. So let’s get started!!

os MODULE:

This is something you should know about before moving on to the good stuff. The os module includes many packages which you can use to basically control your operating system. As you must have already understood, we are going to use this module to access files in your computer.

Like every module, you must import using the import keyword and the name of the module to be able to use it in your program.

See here’s the thing, every module in Python is used for a specific purpose and you can create all of them manually and when sometimes that could be the better option that is not the case most of the times. We use these modules for mainly two reasons. The first one is the obvious one i.e. for convenience. You don’t want to be wasting your time creating something that someone else has already created and provided you with its complete access. The second reason is that these modules have evolved over a period of time and they are still being updated which keeps them up-to-date to almost any change in the tech world. This is somewhat the case with the modules like os. It is better to use these pre-developed modules than to actually code them on your own.

os.path.join():

os.path.join() is an excellent function from the os module. Using this function you can basically create a file path of a specific file. For example,


If you notice, when specifying the root folder (which we will discuss in just a moment), I used two back slashes (\\), the first back slash is used to specify the c:\ drive but the second one is used to escape the first back slash. If you don't know about escaping a character, don't worry I have covered it in this lesson ►►►► PYTHON BEGINNER'S GUIDE | STRINGS IN PYTHON

 A file path is an address that your computer uses to find a specific file. To find your file path just open any folder and look at the bar at the top. That is the file path for that specific folder.


Now you can actually code all of this on your own, but like I said using these pre-developed modules has its own benefits. Like using this function will make your program universal. Now what do I mean by that?? In windows, each folder is separated with single Back Slash(\). Now this is not the case with every operating system. Mac uses a different format for file path and so does Linux. Now creating your own custom program to just create a file path would mean to make your program exclusive to that operating system. I hope you get what I am trying to say.

Now, let’s talk about the root folder. The root folder is basically the parent folder in any operating system, and other files and folders are just its subsidiaries. In simple words, it is the beginning of every file path in an operating system. In windows, it is the C:\ Drive, in Linux it is Home.

CONGRATULATIONS !!! You have learnt about the basics of the 'os' module in Python. Now you know how to use Python to create a specific file path. Trust me this knowledge will help you create programs that are a lot easier to understand and execute. Like I always say, Have fun with it !!! Create some cool programs. Good luck !!!!   

I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T feel free to ask any further queries in the comment section below.


If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:


And do you know the best part? Joining it is FREE !!!

So go ahead click on the link and I will see you there. 

Alright guys, this is it for today’s lesson. I kept it short because I didn't want you to be stressed with all the knowledge in your very first intermediate lesson. Let me know, if you loved it, hated it, want to kill me or any other sort of feedback in the comments section below. Also, if you have any queries regarding the topics taught in this lesson or previous lessons, you can always find me in the comments section below or in the telegram channel where you can personally talk to me and ask me any question about anything we have learnt so far.

So, this was an introduction to working with files using Python. Stay tuned for the second part next week, same time, where we will learn how to open files, read them and edit them using Python. So more cool stuff coming your way, DON’T MISS IT !!

 HOPE YOU HAVE AN AWESOME DAY AHEAD !!! 

Post a Comment

0 Comments