This repository contains two Python scripts to perform basic file operations:
- Listing Files with Specific Extension
- Renaming and Moving Files
This script lists all files with a given extension in a specified directory and saves the filenames (without extensions) to a text file.
Code:
import os
def list_files_with_extension(directory, extension, output_file='file_names.txt'):
with open(output_file, 'w') as f:
for file in os.listdir(directory):
if file.endswith(extension) and file != os.path.basename(output_file):
f.write(file[:-len(extension)] + '\n')
print(f"File names with extension '{extension}' have been saved to '{output_file}'.")
# Example usage
directory = r"C:\Users\amitv\Videos\example"
extension = ".txt"
list_files_with_extension(directory, extension)How it works:
- Lists all
.txtfiles (or any specified extension) in thedirectory. - Saves the filenames (without extensions) into
file_names.txt.
This script renames files following a specific pattern and moves them to a new directory.
Code:
import os
import shutil
# Directories
source_folder = r'C:\Users\amitv\Videos\example'
destination_folder = r'C:\Users\amitv\Videos\example\edited_files'
os.makedirs(destination_folder, exist_ok=True)
for i in range(1, 11):
old_name = f'example{i}.txt'
new_name = f'edit_example{i}.txt'
old_file_path = os.path.join(source_folder, old_name)
new_file_path = os.path.join(destination_folder, new_name)
if os.path.isfile(old_file_path):
shutil.move(old_file_path, new_file_path)
print(f'Renamed and moved: {old_file_path} -> {new_file_path}')
else:
print(f'File {old_file_path} does not exist.')How it works:
- Checks for files named
example1.txttoexample10.txtin thesource_folder. - Renames them to
edit_example1.txttoedit_example10.txt. - Moves the renamed files to the
edited_filesfolder.
- Python 3.x
- No external libraries required (only uses
osandshutilfrom the standard library).
- Update the
directory,source_folder, anddestination_foldervariables to match your desired paths. - Run each script as needed to manage your files.
This code was generated under the guidance of ChatGPT.