Python Forum

Full Version: leading zeros
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
            pass
And 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
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)))
Lovely, thanks!
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:

Error:
Traceback (most recent call last):   File "/home/zoli/PycharmProjects/leadingZeros/inserter.py", line 12, in <module>     os.rename(i, new_filename) FileNotFoundError: [Errno 2] No such file or directory: '5. Variatio 4 a 1 Clav.flac' -> '05. Variatio 4 a 1 Clav.flac'
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.
(Jan-18-2017, 06:38 PM)kerzol81 Wrote: [ -> ]
os.chdir('.')
Is absolutely redundant - you are already in the target directory
I tried this, it doesn't solve it. Thanks though. I know it's not needed to change into the directory
You either need to run the program in the Scott.Ross_Goldberg.Variations folder, or change directories to that folder.
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.