How can I change multiple filenames in different subdirectories in python? - python

I am working with Python3 and am trying to change the names of files in multiple subdirectories to match the folder name. My directory looks like this:
path: C:\Users\Me\Project
In the path, I have the following folders: alldata, folderA, folderB, folderC
FolderA, folderB, and folderC, each contain a file called data.csv
I want to add the letter name of the folder (e.g., A, B, C) to the file (e.g., dataA.csv) inside the folder and then move all these renamed files to "alldata"
I really appreciate the help!

This one might be a little hardcoded, but is probably more understandable for people who are just starting out in Python:
import os
import shutil
# Enter the 'Project' folder
os.chdir('C:\\Users\\Me\\Project')
# Filter for folders that contain the word 'folder'
folders = [folder for folder in os.listdir() if 'folder' in folder]
for folder in folders:
# Get the last letter of every folder
suffix = folder[-1]
# Build source and destination path for the csv files
source = folder + '\\data.csv'
dest = 'alldata\\data' + suffix + '.csv'
shutil.move(source, dest)

The 'os' module in python gives you access to functions that deal with folders and files. For example, there are functions in the os module to move, copy, rename, delete folders and files. Try this for example:
import os
basePath = "C:\\Users\\Me\\Project\\"
# Rename and move the data.csv file in folderA to dataA.csv in the alldata folder
os.rename(basePath + "folderA\\data.csv", basePath + "alldata\\dataA.csv")
# Rename and move the data.csv file in folderB to dataB.csv in the alldata folder
os.rename(basePath + "folderB\\data.csv", basePath + "alldata\\dataB.csv")
# Rename and move the data.csv file in folderC to dataC.csv in the alldata folder
os.rename(basePath + "folderC\\data.csv", basePath + "alldata\\dataC.csv")
# Make sure that they moved as intended
filesInAllDataFolder = os.listdir(basePath + "alldata\\")
print(filesInAllDataFolder)
The os module is super handy and I guarantee you'll use it a lot, so play with it!

This works for me:
import os
def scan_dir(folder):
for name in os.listdir(folder):
path = os.path.join(folder, name)
if os.path.isfile(path):
if 'data' in path:
dir_name = path.split('/')[-2]
new_name_path = path[:-3]+dir_name+'.csv'
new_name_path = new_name_path.split('/')
new_name_path[-2] = 'alldata'
new_name_path = "/".join(new_name_path)
os.rename(path, new_name_path)
else:
scan_dir(path)
directory = 'C:\Users\Me\Project'
scan_dir(directory)

Related

how can I move files from subfolder/sub-subfolder recursively ? [python]

I have problems solving a task. Below you can see my code. I want to work with sub-sub-folders too but my code only moves the sub-folders. How can I do this recursively that it moves all folders in main folder?
path = (r"C:\Users\Desktop\testfolder")
os.chdir(path)
all_files = list(os.listdir())
outputs = os.getcwd()
for files in all_files:
try:
inputs = glob.glob(files + "\\*")
for a in inputs:
shutil.move(a, outputs)
except:
pass
for files in os.listdir("."):
dir_folder = files[:32]
if not os.path.isdir(dir_folder):
try:
os.mkdir(dir_folder)
except:
pass
Here is the new code, that will maintain the folder structure in the output folder as it was in the input folder
from pathlib import Path
import shutil
import os
for path, dir_folder, files in os.walk('./test_folder'):
for file in files:
full_path = os.path.join(path,file)
## Create the path to the subfolder
sub_folder_path = '/'.join(path.split('/')[2:])
# Create output path by joining subfolder path and output directory
output_path = os.path.join(outputs, sub_folder_path)
# Create the output path directory structure if it does not exists, ignore if it does
Path(output_path).mkdir(parents=True, exist_ok=True)
# Move file
shutil.move(full_path, output_path)
You can use os.walk to recursively visit each folder and sub folder and then move all the files in that folder.
import shutil
import os
for path, dir_folder, files in os.walk(input_folder):
for file in files:
full_path = os.path.join(path,file)
move_path = os.path.join(output_path,file)
print(move_path)
shutil.move(full_path, move_path)
print(file)
Where output_path is the path to the folder you move to move the files to.
This will not create the same folder structure in output folder, just move all the files there, do you also want to maintain structure in output folder? If so, I can edit my answer to reflect that

Saving Multiple Local/Offline Files to Target Folder with OS Module

I am trying to move all files that are in Just source_path to the target_path.
Here is the code:
import os
target_path = 'C:/Users/Justi/source/repos/GUI Developmentv3/Test 02' + '\\'
source_path ='C:/Users/Justi/source/repos/GUI Developmentv3/' + '\\'
for path, dir, files in os.walk(source_path):
if files:
for file in files:
if not os.path.isfile(target_path + file):
os.rename(path + '\\' + file, target_path + file)
However, this also moves other files that are in the sub-directory folders of the source folder into the target folder.
May I check how I could change the code to only move files at the exact directory of source_path without affecting files in source_path's sub-dictionary? Thank you in advance.
I think the problem is the usage of os.walk, because it walks the whole tree. See the docs:
Generate the file names in a directory tree by walking the tree either top-down or bottom-up.
You are only interested in the contents of a particular folder, so os.listdir is more suitable:
Return a list containing the names of the entries in the directory given by path.
The following code will iterate over the files in the source dir, and move only the files into the target dir.
import os
import shutil
source = "source"
target = "target"
for f in os.listdir(source):
print(f"Found {f}")
if os.path.isfile(os.path.join(source, f)):
print(f"{f} is a file, will move it to target folder")
shutil.move(os.path.join(source, f), os.path.join(target, f))

Make a loop that will take the same name pairs from two folders?

One main folder that has one folder named old and another called new
The old has some folders. The new has a few of these folders with same names and nothing more.
I want to delete the folders of the old that are not present in the new first and then: make a loop that will take each file -same name -pair, and put it in the
the following line:
arcpy.Append_management(["shpfromonefolder.shp", "shpfromsecondfolder.shp"],"NO_TEST")
for example: land.shp from one folder with land.shp from the other folder so it will be:
arcpy.Append_management(["land.shp", "land.shp"],"NO_TEST")
This will delete folders in old_path if they exist do not in new_path:
import os
import shutil
old_path = r"old file path"
new_path = r"old file path"
for folder in os.listdir(old_path):
if folder not in os.listdir(new_path):
shutil.rmtree(os.path.join(old_path, folder))
This will find the matching shape files and pass them to arcpy.Append_management():
import os
import arcpy
for dir_path, dir_names, file_names in arcpy.da.Walk(workspace=new_path, datatype="FeatureClass"):
for filename in file_names:
new_file_path = os.path.join(dir_path, filename)
folder = os.path.basename(os.path.dirname(new_file_path))
old_file_path = os.path.join(old_path, folder, filename)
if os.path.exists(old_file_path):
arcpy.Append_management(inputs=[new_file_path], target=old_file_path, schema_type="NO_TEST")

Compare file names of two directories and move matched files to 3rd directory

I have 3 folders. 1)Content 2) Shared 3)MoveHere
I have 50 txt files in "Content" folder and 300 text files in "Shared" folder.
here, names of 50 txt files in "Content" folder matches with names of files in "Shared" folder.
What we need here is, move those matched files from "Shared" folder to MoveHere folder.
I tried below code, but it did not work.
#!/usr/bin/env python
import os
import shutil
# Get current working directory
CD = os.getcwd()
SUD = '/D:/TestScript/shareduser/'
DEST = '/D:/TestScript/MoveHere/'
# Get a list of files in the current working directory
for file_CD in os.listdir(CD):
file_name_in_Content = os.path.basename(os.path.splitext(file_CD)[0])
#print('CWD '+ file_name_in_Content)
for file_SUD in os.listdir(SUD):
file_name_in_SharedUser = os.path.basename(os.path.splitext(file_SUD)[0])
if file_name_in_Content == file_name_in_SharedUser:
SRC_FULL_PATH = SUD + "/" + file_SUD
DEST_FULL_PATH = DEST + "/" + file_SUD
shutil.move(SRC_PATH, DEST_PATH)
print ("\nDone")
I appreciate all your help.

How do i copy files with shutil in python

im trying to copy a .txt file from one dest to another. This code is running but the file isnt copying. What am i doing wrong?
import shutil
import os
src = "/c:/users/mick/temp"
src2 = "c:/users/mick"
dst = "/c:/users/mick/newfolder1/"
for files in src and src2:
if files.endswith(".txt"):
shutil.copy(files, dst)
Your for loop isn't actually searching through the files of each of your sources. In addition your for loop isn't looping through each of the sources but rather the letters in src and src2 that are present in both. This adjustment should handle what you need.
import os
src = ["c:/users/mick/temp", "c:/users/mick"]
dst = "c:/users/mick/newfolder1/"
for source in src:
for src_file in os.listdir(source):
if src_file.endswith(".txt"):
old_path = os.path.join(source, src_file)
new_path = os.path.join(dst, src_file)
os.rename(old_path, new_path)
You shouldn't need shutil for this situation as it is simply a more powerful os.rename that attempts to handle different scenarios a little better (to my knowledge). However if "newfolder1" isn't already existant than you will want to replace os.rename() with os.renames() as this attempts to create the directories inbetween.
shutils is useful for copying, but to move a file use
os.rename(oldpath, newpath)
I know the question is little older. But Here are the simple way to achieve your task. You may change the file extension as you wish. For copying you may use copy() & for moving yo may change it to move(). Hope this helps.
#To import the modules
import os
import shutil
#File path list atleast source & destination folders must be present not everything.
#*******************************WARNING**********************************************
#Make sure these folders names are already created while using the 'move() / copy()'*
#************************************************************************************
filePath1 = 'C:\\Users\\manimani\\Downloads' #Downloads folder
filePath2 = 'F:\\Projects\\Python\\Examples1' #Downloads folder
filePath3 = 'C:\\Users\\manimani\\python' #Python program files folder
filePath4 = 'F:\\Backup' #Backup folder
filePath5 = 'F:\\PDF_Downloads' #PDF files folder
filePath6 = 'C:\\Users\\manimani\\Videos' #Videos folder
filePath7 = 'F:\\WordFile_Downloads' #WordFiles folder
filePath8 = 'F:\\ExeFiles_Downloads' #ExeFiles folder
filePath9 = 'F:\\Image_Downloads' #JPEG_Files folder
#To change the directory from where the files to look // ***Source Directory***
os.chdir(filePath8)
#To get the list of files in the specific source directory
myFiles = os.listdir()
#To move all the '.docx' files to a different location // ***Destination Directory***
for filename in myFiles:
if filename.endswith('.docx'):
print('Moving the file : ' + filename)
shutil.move(filename, filePath4) #Destination directory name
print('All the files are moved as directed. Thanks')
import os, shutil
src1 = "c:/users/mick/temp/"
src2 = "c:/users/mick/"
dist = "c:/users/mick/newfolder"
if not os.path.isdir(dist) : os.mkdir(dist)
for src in [src1, src2] :
for f in os.listdir(src) :
if f.endswith(".txt") :
#print(src) # For testing purposes
shutil.copy(src, dist)
os.remove(src)
I think the error was that you were trying to copy a directory as a file, but you forgot to go through the files in the directory. You were just doing shutil.copy("c:/users/mick/temp/", c:/users/mick/newfolder/").

Categories