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:
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
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|
You can use this function to close a file.
Flushes the internal buffer, like stdio’s fflush. This could be a no-op on some file-like objects.
Gives file descriptor that is used by the underlying implementation to request I/O operations from the operating system. Returns an integer
Returns True if the file is connected to a tty(-like) device. Otherwise, return false
Returns the next line from the file each time it is called.
Reads at most the specified size bytes from the file (less if itreaches EOF before obtaining size bytes).
Reads one entire line of data from the file. Places a trailing in the string.
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.
Sets the file’s current position to the specified offset
Returns the current file’s position
Truncates the size of the file. If the optional size argument is present, the file is truncated to (at most) the given size.
Use this to writes a string onto the file. Does not return anything.
Writes a sequence of strings onto the file. The sequence may be any iterable object that produces strings, typically a list of strings.