In this lesson we are going to learn different file operations such as how to rename and delete a file. We would also learn how to work with folders.
We would cover the following:
1. Renaming and Deleting a File
The os module provides you with methods for performing various operations on file. For example, renaming a file or deleting a file.
However, to use this methods, you must import the os module using the command:
import os
The rename() Method
You use the rename() method to change the name of a file. The rename() method has two parameters. The current file_name and the new file_name.
For example, the code below changes the filename from old_filename.txt to new_filename.txt
import os # change the filename os.rename("old_filename, new_filename.txt")
The remove() Method
You can use the remove() for deleting files. The remove() methods takes only one parameter, that is the name of the file to be deleted.
For example, the code below deletes the file named myfile.txt
import os # delete the file from disk os.remove("myfile.txt")
2. Working with Directories
Files are contained in directories(folders). The os module provides various functions to help you handle directories in Python. We would consider three operations:
- Create a directory
- Delete a directory
- Change a directory
The mkdir() Method
You can use mkdir() method to create new directory. When you use this method to create a new directory, the directory is created in the current directory. You need to provide the name of the directory to be created as argument to the mkdir() method.
For example, the code below creates a directory named myfiles. I recommend you try it out.
import os # create a new directory in the current path os.mkdir("myfiles")
The chdir() Method
You use the chdir() method to change the current directory. You must provide the name of the directory you want to set as the current directory.
For example, the code below changes the current directory to D:\data
import os # change the current directory to D:/data os.chdir("D:/data")
The getcwd() Method()
You use this method to display the current working directory.For example
os.getcwd()
The rmdir() Method
You can use the rmdir() to delete a directory from the disk. This method takes as parameter, the name of the directory you want to delete.
For example, the code below deletes the directory “D:/data”
import os # delete the directory D:/data os.rmdir("D:/data")
3. Python File Methods
The are several function that you can use for file operations in python. Once you create a file object, then you can use these functions to manage them.
Find the list below. I recommend you try each of them.
| SN. | Methods and brief description |
|---|---|
| 1 | file.close() You can use this function to close a file. |
| 2 | file.flush() Flushes the internal buffer, like stdio’s fflush. This could be a no-op on some file-like objects. |
| 3 | file.fileno() Gives file descriptor that is used by the underlying implementation to request I/O operations from the operating system. Returns an integer |
| 4 | file.isatty() Returns True if the file is connected to a tty(-like) device. Otherwise, return false |
| 5 | file.next() Returns the next line from the file each time it is called. |
| 6 | file.read([size]) Reads at most the specified size bytes from the file (less if itreaches EOF before obtaining size bytes). |
| 7 | file.readline([size]) Reads one entire line of data from the file. Places a trailing in the string. |
| 8 | file.readlines([sizehint]) Reads until the EOF using readline() and return a list containing the lines. If the optional sizehint argument is provided, then instead of reading up to EOF, whole lines totaling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. |
| 9 | file.seek(offset[, whence]) Sets the file’s current position to the specified offset |
| 10 | file.tell() Returns the current file’s position |
| 11 | file.truncate([size]) Truncates the size of the file. If the optional size argument is present, the file is truncated to (at most) the given size. |
| 12 | file.write(str) Use this to writes a string onto the file. Does not return anything. |
| 13 | file.writelines(sequence) Writes a sequence of strings onto the file. The sequence may be any iterable object that produces strings, typically a list of strings. |

Join Joe Marini for an in-depth discussion in this video Reading and writing files, part of Learning Python