Python Forum
Decompressing bz2 in multiple sub-directories
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decompressing bz2 in multiple sub-directories
#11
You can use auxiliary variable, initialize it to 1 before your first "files" for statement and use it to create filename and increase after use.

for dirpath, dirname, ...
    file_number = 1
    for filename in ..
      ....

      new_filename = "{:03d}.json".format(file_number)  # you can use just str() + ".json", but format adds more options
      file_number += 1
      newfilepath = os.path.join(dirpath, new_filename)
      ...
If you have some old files with same name, it will overwrite them, so be careful. And with "numerical" file names its sometimes useful to zero-fill number, so they are sorted naturally (no 1.json, 11.json, ..19.json, 2.json, but 001.json, 002.json...).
Reply


Messages In This Thread
RE: Decompressing bz2 in multiple sub-directories - by zivoni - Mar-29-2017, 04:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 467 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Listing directories (as a text file) kiwi99 1 858 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,208 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 731 Nov-04-2022, 05:23 PM
Last Post: elnk
  I need to copy all the directories that do not match the pattern tester_V 7 2,498 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Moving specific files then unzipping/decompressing christophereccles 2 2,397 Apr-24-2021, 04:25 AM
Last Post: ndc85430
  Python create directories within directories mcesmcsc 2 2,241 Dec-17-2019, 12:32 PM
Last Post: mcesmcsc
  How to combine file names into a list from multiple directories? python_newbie09 3 5,249 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  Accessing files in various directories and folders ccuny 2 2,190 May-08-2019, 12:11 PM
Last Post: ccuny
  Creating directories from two lists QueenSveta 2 2,797 Jun-22-2018, 09:33 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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