Table of Contents
In this Python tutorial, we will cover copying, creating, moving, and deleting files in Python. Manipulating files is a great skill to have to archive files after x amount of days, move files after each business week or create a back-up of important files. In addition, manipulating files can be used to store data, configurations for users, images, and videos in software applications. Python provides a vast amount of built-in methods to manipulate files easily with a small amount of code. We will cover a few basic ways to manipulate files in Python.
Create Files in Python
Python allows multiple modes in creating a file. The modes are dependent on what kind of data you will be reading and writing to or from the file.The built-in open() function in Python is used when opening a file.
Note: In order to find the path in Windows it must be double forward slashes (//) and Linux is a single forward slash (/).
Python File Modes
Below are some Python modes for reading or writing files:
- r : Opens a file reading only.
- rb : Opens a file for reading in Binary format.
- w : Opens a file for writing and creates a new file if it doesn’t yet exist. In the case that the file does exist, it overwrites it.
- w+ : Opens a file for writing but also for reading and creating it if it doesn’t exist. If a file already exists, it overwrites it.
- wb : Opens a file for writing in Binary format.
- wb+ : Opens a file for writing and reading in Binary format.
- a : Opens a file for appending at the end of the file.
- + : In general, this character is used along side r, w, or a and means both writing and reading.
Note: r will be assumed default id no file mode is specified when opening a file
open() function
The python open() function with the w+ mode will allow for creating a file.
file = open("path/to/file/file.txt", "w+")
The above example is very basic to create a file in a designated path. The required fields are the filename and path if necessary. If you want the filename in the same location as the script, then no path is necessary, but in my example I created a file in a different path. Also, the mode is optional but it would default to r mode. In order to create a file, w+ mode was needed and the r mode would not be helpful reading a file that doesn’t exist.
close() function
Every time you open a file in Python, making sure the file is closed is very important. The close() function will close the file that was opened. Another option is opening the file with the keyword with will close the file automatically. System resources will be used every time a file is opened and can cause program slowness. In addition, if a file is not closed, this will prevent other programs to open the file if already in use by another program.
Input:
file = open("path/to/file/file.txt", "w") file.write("We are writing into the file.") file.close() file = open("path/to/file/file.txt", "r") print(file.read()) file.close()
Output:
We are writing into the file.
Copy Files
The shutil module offers many high-level operations on files and file collection. One of these functions is using the shutil.copy function to copy files from one directory into another. I frequently use this script to copy files to an archive and then remove the files from the original directory. It’s a very simple script to create in Python but very beneficial.
Some of these high-level operations include functions support file copying, moving and removal from a set path.
Input:
import os, shutil path = "C://FileFrom//" move_path = "C://FileTo//" copy_file = open("C://FileFrom//copy_file.txt", "w") copy_file.write("We are writing into the copy file.") copy_file.close() files = os.listdir(path) files.sort() for file in files: print(f"Copying file: {file} ") old_path = path + file new_path = move_path + file shutil.copy(old_path,new_path)
Output:
Copying file: copy_file.txt
Move Files
The shutil module offers a function to move files from one directory into another and the function is shutil.move in Python. If you are looking to move all files from a directory. If you need to only move files with a certain name or files older than 7 days, this will need to be specified.
Input:
import os, shutil path = "C://FileFrom//" move_path = "C://FileTo//" move_file = open("C://FileFrom//move_file.txt", "w") move_file.write("We are writing into the move file.") move_file.close() files = os.listdir(path) files.sort() for file in files: print(f"Moving file: {file} ") old_path = path + file new_path = move_path + file shutil.move(old_path,new_path)
Output:
Moving file: copy_file.txt Moving file: move_file.txt
As you can see from the output, the shutil.move function moves both files and not only the newly created move_file.txt.
Delete Files
The os module offers a remove function that we can use to delete (remove) files in a directory. If you are only looking to remove a certain file after a certain date, then this will need to be specified. The below script will remove all files in a directory and be cautious on your settings of folders which could be removed as well.
Input:
import os file1 = open("C://FileFrom//file1.txt", "w") file1.write("We are writing into the newly created file1.") file1.close() file2 = open("C://FileFrom//file2.txt", "w") file2.write("We are writing into the newly created file2.") file2.close() remove_path = "C://FileFrom//" removeFiles = [os.path.join(remove_path,file) for file in os.listdir(remove_path)] for file in removeFiles: print(f"Removing file: {file} ") os.remove(file)
Output:
Removing file: C://FileFrom//file1.txt Removing file: C://FileFrom//file2.txt
I hope this Python tutorial was helpful in learning how to create, copy, move and delete files in Python.