I amt trying to find all relevant files and folders from local repository. e.g. I pulled projects from git to my local machine and I want to search for specific files(.txt) and save them to new folder in same pattern (root->dir-> file). Basically I want to get rid of any files which doesn't match name and extension but keep the same format. Thanks in advance.
How to find files and save them to different folder in python
728 views Asked by user2661518 At
2
There are 2 answers
0
On
you can do this:
import shutil
import os
The path of the folder where the files are:
path = r'C:\Users\pc\Downloads\6.ahmed'
code to select specific files, here I'm selecting all files in the folder with .xml extension. You can change it according to your need.
xml_files = [f for f in os.listdir(path) if f.endswith('.xml')]
source and destination folder path and the loop to move the files:
source_folder = r"C:\Users\pc\Downloads\6.ahmed\\"
destination_folder = r"C:\Users\pc\Downloads\annotations\\"
# iterate files
for file in xml_files:
# construct full file path
source = source_folder + file
destination = destination_folder + file
# move file
shutil.move(source, destination)
print('Moved:', file)
combine the whole code and run it.
You can traverse the directory tree finding the files you want with this:
then copy the repository and do something like this: