- Python Copy Videofiles
- Story 1 - Copying from a single directory
- Story 2 - Copying from multiple directories
- Story 3 - Copying with existing file check
- Functionality
- browse_src_dirs()
- browse_dst_dir()
- select_additional_files()
- expand_src_dirs()
- expand_additional_files()
- expand_dst_dir()
- copy_files()
- User Interface
- How Does it works?
- Requirements
- Usage
- search_and_copy() Function
- Input Parameters
- Output
- Implementation
- main() Function
- Implementation
- Dependencies
- copy_files_gui() function
- Support
- License
This is a Python program that allows the user to copy video files from a source directory and any additional directories to a destination directory using a GUI created with the tkinter library.
This will search for all .mp4 files in the Test1 directory and copy them to the Output directory.
src_dirs = ['C:/Videos/Test1']
dst_dir = 'C:/Videos/Output'
search_and_copy(src_dirs, dst_dir)
This will search for all .mp4 files in the Test1 and Test2 directories and copy them to the Output directory.
src_dirs = ['C:/Videos/Test1', 'C:/Videos/Test2']
dst_dir = 'C:/Videos/Output'
search_and_copy(src_dirs, dst_dir)
This will search for all .mp4 files in the Test1 and Test2 directories and copy them to the Output directory only if they do not already exist in the Output directory.
src_dirs = ['C:/Videos/Test1', 'C:/Videos/Test2']
dst_dir = 'C:/Videos/Output'
search_and_copy(src_dirs, dst_dir, check_existing=True)
The program has several functions to enable the user to browse and select the source and destination directories and any additional directories to include in the copy process.
-browse_src_dirs()
: This function opens a file dialog and prompts the user to select a source directory. The path to the selected directory is saved in the global variable src_dirs
browse_dst_dir()
: This function opens a file dialog and prompts the user to select a destination directory. The path to the selected directory is saved in the global variable dst_dirselect_additional_files()
: This function prompts the user to enter the number of additional directories they want to include in the copy process and then prompts them to select those directories using a file dialog. The selected directories are stored in the additional_files listexpand_src_dirs()
,expand_additional_files()
, andexpand_dst_dir()
: These functions open new windows that display the contents of the corresponding directoriescopy_files()
: This function copies all .mp4 files from the source directory and any additional directories to the destination directory. If a file with the same name already exists in the destination directory, the program skips the copy. The function updates the status label to indicate how many files were copied and if there were any errors
The user interface is created with the tkinter library and includes labels, entry fields, and buttons to interact with the functions described above. The program prompts the user to select a source directory, a destination directory, and any additional directories to include in the copy process. It also displays the status of the copy process.
- The program prompts the user to select a source directory and a destination directory, as well as any additional directories that they want to include in the copy process.
- The
browse_src_dirs()
andbrowse_dst_dir()
functions prompt the user to select a directory using a file dialog and save the path to the selected directory in the src_dirs and dst_dir global variables, respectively. - The
select_additional_files()
function prompts the user to enter the number of additional directories they want to include in the copy process and then prompts them to select those directories using a file dialog. The selected directories are stored in the additional_files list. - The
expand_src_dirs()
,expand_additional_files()
, andexpand_dst_dir()
functions open new windows that display the contents of the corresponding directories. - The
copy_files()
function copies all .mp4 files from the source directory and any additional directories to the destination directory. If a file with the same name already exists in the destination directory, the program skips the copy. The function updates the status label to indicate how many files were copied and if there were any errors. - The main part of the code creates a tkinter GUI with labels, entry fields, and buttons to interact with the functions described above.
Python 3.6 or later Required packages listed in requirements.txt To install the required packages, run the following command in your terminal:
pip install -r requirements.txt
To run the script, use the following command in your terminal:
python gui.py
Once the GUI is open, use the "Select Source Directory", "Select Additional Directories", and "Select Destination Directory" buttons to choose the directories to include in the copy process. Use the "Expand" buttons to view the contents of the selected directories. Finally, click the "Copy Files" button to copy all .mp4 files from the selected directories to the destination directory.
The search_and_copy()
function is used to search for all .mp4
files in the specified source directories and copy them to the specified destination directory. The function checks whether each file has already been copied, and only copies it if it hasn't been.
The search_and_copy() function takes two input parameters:
src_dirs
: A list of source directories to search for .mp4 files.dst_dir
: The destination directory to copy the files to.
The search_and_copy()
function doesn't return any output. Instead, it prints a message to the console indicating whether any files were copied.
The search_and_copy()
function is implemented as follows:
- Initialize the
copied
variable toFalse
to indicate that no files have been copied yet. - Read the contents of the
files.txt
file to get a list of all files that have already been copied. This file is used to keep track of which files have already been copied to the destination directory. - For each source directory specified in
src_dirs
, search for all.mp4
files in the directory and its subdirectories using theos.walk()
function. - For each
.mp4
file found, check whether it has already been copied to the destination directory by checking whether its name appears in thefile_names
list. - If the file hasn't been copied yet, copy it to the destination directory using the
shutil.copy()
function and update thefiles.txt
file with the name of the copied file. - If at least one file was
copied
, set the copied variable toTrue
. - If no files were copied, check whether all
.mp4
files in the source directories have already been copied to the destination directory. If so, print a message to the console indicating that all files have already been copied.
The main()
function is used to prompt the user to specify the source directories and destination directory, and then call the search_and_copy()
function to copy the .mp4
files.
The main()
function is implemented as follows:
- Prompt the user to enter the paths of the source directories to search for
.mp4
files, separated by commas. - Split the user input into a list of directories using the
split()
function. - Prompt the user to enter the path of the destination directory to copy the files to.
- Call the
search_and_copy()
function with the source directories and destination directory as input parameters
The code uses the following Python modules:
os
: Used to search for files in directories and subdirectories.shutil
: Used to copy files.tkinter
: Used to create the GUI.
- The copy_files_gui() function is called when the Copy files button is clicked.
- It retrieves the values of the source directory, destination directory, and additional directories entry fields using the get() method.
- It calls the search_and_copy() function with the source directories and destination directory as arguments.
- It updates the status label to indicate how many files were copied and if there were any errors.
The script requires user to select directories in the guy.exe:
- Source Directories
- Additional Directories (Optional)
- Destination Directory
Diana-Maria Iercosan |
For any questions or support, please contact me via https://hurr13ane.com/contact/
This project is licensed under the MIT License.