Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
leading zeros
#8
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.
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 744 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Removing leading\trailing spaces azizrasul 8 2,796 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,797 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 2,037 Aug-26-2022, 09:27 PM
Last Post: markanth
  remove zeros at the end of a number Frankduc 7 2,208 Feb-25-2022, 03:48 PM
Last Post: Frankduc
  Solving for zeros of an equation! fmitchell17 0 1,858 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,624 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,208 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