I am still very new to Python (3). I have a BUNCH of sensor data, but the download limit forces me to retrieve the data in chunks instead of all at once (each .zip file downloaded contains a folder of .csv files for each sensor's data during a given time period). Thus, I have dozens of large .csv files distributed among several folders that I would eventually like to concat/merge/append into one .csv file for each sensor's full data. To make things more complicated, .csv file names for each sensor are identical across the folders. I have developed the following code to rename and move the files into one folder so that later I can concat/merge/append. It works fine except for the fact that the number I am inserting into the new file name is not incrementing.
import os
path = r"C:\directory\sensordatafolders" #folders with .csv files
newPath = r"C:\directory\new" #destination for renamed files
for root, dirs, files in os.walk(path):
for name in files:
base, extension = os.path.splitest(name)
if not os.path.exists(os.path.join(newPath, base + extension))
oldfile = os.path.join(os.path.abspath(root), name)
newfile = os.path.join(newPath, base + extension)
os.rename(oldfile, newfile)
else:
i = 1
oldfile = os.path.join(os.path.abspath(root), name)
newfile = os.path.join(newPath, base + '_' + str(i) + extension)
i +=1
os.rename(oldfile, newfile)
After the second loop (*.csv and *_1.csv files successfully moved), it gives me the 'cannot create a file when that file already exists' error. This is because (I think) it keeps trying to create *_1.csv files instead of incrementing to *_2.csv, etc..
Your
i = 1
After else should not be there, it keeps setting i to 1 thus always making i's value in to a 2, try to have it outside of the for statements
来源:https://stackoverflow.com/questions/49700561/incrementing-number-in-file-name-when-file-exists