Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
leading zeros
#1
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
Reply


Messages In This Thread
leading zeros - by kerzol81 - Jan-18-2017, 06:38 PM
RE: leading zeros - by buran - Jan-18-2017, 06:53 PM
RE: leading zeros - by kerzol81 - Jan-18-2017, 06:59 PM
RE: leading zeros - by kerzol81 - Apr-23-2017, 12:21 PM
RE: leading zeros - by volcano63 - Apr-23-2017, 12:24 PM
RE: leading zeros - by kerzol81 - Apr-23-2017, 12:38 PM
RE: leading zeros - by ichabod801 - Apr-23-2017, 12:40 PM
RE: leading zeros - by kerzol81 - Apr-23-2017, 03:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing leading whitespaces palladium 1 745 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Removing leading\trailing spaces azizrasul 8 2,805 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,802 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 2,041 Aug-26-2022, 09:27 PM
Last Post: markanth
  remove zeros at the end of a number Frankduc 7 2,210 Feb-25-2022, 03:48 PM
Last Post: Frankduc
  Solving for zeros of an equation! fmitchell17 0 1,861 Apr-05-2021, 07:49 PM
Last Post: fmitchell17
  Dataframe Removes Zeros JoeDainton123 2 4,442 Sep-17-2020, 12:47 PM
Last Post: scidam
  How to keep leading zeros with pandas? eeps24 1 6,627 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,209 May-08-2020, 03:14 PM
Last Post: DPaul
  leading zero number formatting RedSkeleton007 3 3,974 Jan-27-2019, 04:56 PM
Last Post: RedSkeleton007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020