Python file search
Searching for files in a directory can be a daunting task, especially when working with a large number of files. The "glob" module in Python makes it easy to search for files using wildcard characters.
import glob
# look for files with the .txt extension
txt_files = glob.glob("*.txt")
print(f"txt files: {txt_files}")
Here's a snippet of Python code that uses the glob module to look for files with the .txt extension in the current directory.
See how it works:
The glob.glob() function returns a list of filenames that match the specified pattern.
In this case, the default is "*.txt", which matches all files in the current directory that have the .txt extension.
The function stores the list of matching file names in the variable files_txt.
Finally, the code prints the list of file names with the .txt extension using an f-string.
Note: If you want to search for files with a different extension or in a different directory, you can modify the string argument passed to the glob.glob() function.
For example, glob.glob("/path/to/directory/*.csv") would search for all CSV files in the directory located at /path/to/directory/.
Moving Files in Python
Manually moving files from one folder to another can be a tiring task. With Python, it's possible to move files from one folder to another with just a few lines of code.
To move files, we will use the shutil module and its move function. The example below moves all files from one folder to another:
import shutil
import os
source_directory = "path/of/source/directory"
destination_directory = "path/of/destination/directory"
for file_name in os.listdir(source_directory):
source_path = os.path.join(source_directory, file_name)
destination_path = os.path.join(destination_directory, file_name)
shutil.move(source_path, destination_path)
This is a snippet of Python code that uses the shutil and os modules to move all files from a source directory to a destination directory.
See how it works:
- First, the source_directory and destination_directory variables are defined to store the paths to the source directory and destination directory, respectively.
- Then a for loop is used to iterate over all the filenames in the source directory using the os.listdir(source_directory) function.
- For each filename, the source_path and destination_path variables are set using the os.path.join() function, which concatenates the directory path with the filename to create a complete path for each file.
- Finally, the shutil.move() function is used to move each file from source_path to destination_path.
Note: This code should be used with caution as it moves all files from the source directory to the destination directory. Make sure you really want to move these files before running the code. Also, it is important to have permission to write files to the destination directory.
Copying Files in Python
Sometimes, instead of moving files, it may be necessary to copy a file to another folder. To copy files, we can use the shutil module and its copy function.
The example below copies all files from one folder to another:
import shutil
import os
source_directory = "path/of/source/directory"
destination_directory = "path/of/destination/directory"
for file_name in os.listdir(source_directory):
source_path = os.path.join(source_directory, file_name)
destination_path = os.path.join(destination_directory, file_name)
shutil.copy(source_path, destination_path)
This is a snippet of Python code that uses the shutil and os modules to copy all files from a source directory to a destination directory.
See how it works:
- First, the source_directory and destination_directory variables are defined to store the paths to the source directory and destination directory, respectively.
- Then a for loop is used to iterate over all the filenames in the source directory using the os.listdir(source_directory) function.
- For each filename, the source_path and destination_path variables are set using the os.path.join() function, which concatenates the directory path with the filename to create a complete path for each file.
- Finally, the shutil.copy() function is used to copy each file from source_path to destination_path.
Note: This code should be used with care as it copies all files from the source directory to the destination directory. Make sure you really want to copy these files before running the code. Also, it is important to have permission to write files to the destination directory.
Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source
Article link:http://pybeginners.com/article/python-automation-search-move-and-copy-files-in-python/