![]() |
leading zeros - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: leading zeros (/thread-1661.html) |
leading zeros - kerzol81 - Jan-18-2017 Hi, A wrote a script that moves files into folders according to the files creation time, but I could not figure out how to add a leading zero to the folders name. I'd like it this way: 2017-01-18, not 2017-1-18. import os, time, shutil os.chdir('.') for f in os.listdir('.'): file_creation_time = time.gmtime(os.path.getmtime(f)) dir_creation_time = str(file_creation_time.tm_year) + '-' + str(file_creation_time.tm_mon) + '-' + str(file_creation_time.tm_mday) if not os.path.isdir(dir_creation_time): os.mkdir(dir_creation_time) if '.py' in f: continue else: try: shutil.move(f, dir_creation_time) except: passAnd I'd like a more elegant way of excluding the the script, but sys.argv[0] returns the full path, but I only need the script name. I also wanted the first argument to be the folder path on which folder the script should run, i tried this at the beginning: dir = sys.argv[1] os.chdir(dir)But it didn't work. I'd really like to invoke the script by cron, with an argument on which folder should it run... Thanks Zoli RE: leading zeros - buran - Jan-18-2017 replace file_creation_time = time.gmtime(os.path.getmtime(f)) dir_creation_time = str(file_creation_time.tm_year) + '-' + str(file_creation_time.tm_mon) + '-' + str(file_creation_time.tm_mday)with dir_creation_time = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(f))) RE: leading zeros - kerzol81 - Jan-18-2017 Lovely, thanks! RE: leading zeros - kerzol81 - Apr-23-2017 Hi, I have some flac files without leading zeros: https://pastebin.com/cEVpqkSu I did this code: import os folder = "Scott.Ross_Goldberg.Variations" for i in os.listdir(folder): filename, extension = (os.path.splitext(i)) filenumber = (filename.split('.')[0].zfill(2)) filename = (filename.split('.')[1]) new_filename =('{}.{}{}'.format(filenumber, filename, extension)) print(new_filename) #os.rename(i, new_filename)Interestingy if I print them, they look good, but if I would like to rename them, the scripts gives me an error: Obviously I did some mistake, could you help me out? First I was suspicious of whitespace characters in filenames, but i don't think that's the case. The files are there in the folder.
RE: leading zeros - volcano63 - Apr-23-2017 (Jan-18-2017, 06:38 PM)kerzol81 Wrote:Is absolutely redundant - you are already in the target directoryos.chdir('.') RE: leading zeros - kerzol81 - Apr-23-2017 I tried this, it doesn't solve it. Thanks though. I know it's not needed to change into the directory RE: leading zeros - ichabod801 - Apr-23-2017 You either need to run the program in the Scott.Ross_Goldberg.Variations folder, or change directories to that folder. RE: leading zeros - kerzol81 - Apr-23-2017 I figured it out. The error was that the folder var needed a full path... import os folder = "/home/zoli/PycharmProjects/leadingZeros/Scott.Ross_Goldberg.Variations/" os.chdir(folder) for i in os.listdir(folder): filename, extension = (os.path.splitext(i)) filenumber = (filename.split('.')[0].zfill(2)) filename = (filename.split('.')[1]) new_filename =('{}.{}{}'.format(filenumber, filename, extension)) #print(new_filename) os.rename(i, new_filename)Thanks for your help, I really needed to change into that directory. What I don't understund is, that if the script was able to change into that dir, and print the corrected filenames, why it needed an absolute path for the renaming. |