How to create, read, save files using Python | applied electronics engineering

Latest

How to create, read, save files using Python

By Applied Electronics - Monday, April 10, 2017 No Comments
No programming with ability to create, read and save files would be useful. Python programming language is similar to other programming language and supports the ability to work with different kinds of files. In this tutorial we will show briefly how to create, read, save files using Python.

But before we show how to read and write to actual files with python we will show some of the important functions that has to do with files. This has to do with path of the file, how to know where you are, how to change the directory or folder, then how to make a new directory etc. In the next tutorial we show how to actually create a file, read it and save it using python.

As you know a file saved in computer hard disk has a path and file name saved with some extension. The path is made of backslash on windows OS and forward Slash on OS X and Linux as the separator
between folder names. That is important thing to know because while writing program you have to write forward or backward slash. With python programing language this is easy because we can use the os.path.join() function to work on all operating systems to handle both cases.

For example of using the os.path.join() function, suppose that your file is located in C:\python\examples\test.py. Then you would use the os.path.join() function in the following way.

import os
os.path.join('python','examples','test')

The result is the following string,

'python\\examples\\test'

And this is done in windows OS. In linux machines the string would have been 'python/examples/test'.

The os.path.join() function is helpful if you need to create strings for filenames. For example,

myFiles = ['accounts.txt', 'details.csv', 'invite.docx']
for filename in myFiles:
          print(os.path.join('C:\\Users\\john', filename))

We get the result,

C:\Users\john\accounts.txt
C:\Users\john\details.csv
C:\Users\john\invite.docx

The Current Working Directory

Next thing you need to know about when working with files is the current working directory or cwd. Every programming language like python supports the current working directory to know where at this point is the current working directory. So in python you would get the current working directory as follows,

import os
os.getcwd()

The result is something like this,

'C:\\Python34'

Next thing you need to know about when working with files is the how to change directory. To change the directory you would use the os.chdir() function. An example is as follows.

os.chdir('C:\\Windows\\System32')
os.getcwd()
'C:\\Windows\\System32'

Creating a directory(folder)


How do you create a new directory or folder using python programming language? The following program code shows how to create a new folder with python.

import os
os.makedirs('C:\\python\\examples\\test_folder')

This creates a new folder called test_folder.

So in this way you can create directory, change directory, create strings for the directory path.

See also 3 steps to reading text files in python and How to search for specific pattern in a text using Python.

No Comment to " How to create, read, save files using Python "