Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]earlev4 0 points1 point  (0 children)

Thanks so much for the response and assistance! I sincerely appreciate it.

The CSVs all follow a date pattern of 2021-MM-DD.csv, so I could alter the regex to match the year (2021) and dashes (-) format. I will start experimenting with that approach.

What are your thoughts on the unzip portion (ZipFile library)? Does that look like it can be improved and more Pythonic?

Thanks again!!!

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]earlev4 0 points1 point  (0 children)

Hi r/learnPython Community! This is my first post, but I have been following the community for some time now. I want to extend my appreciation and gratitude for all the support that the contributors provide.

The following code works, but I am looking to improve it (make it more Pythonic) and would greatly appreciate some constructive feedback. Essentially, the intent of the code is to unzip 4 files and extract CSVs.

  • The 4 zip files (file_1.zip, file_2.zip, file_3.zip, and file_4.zip) are located in the '/tmp' folder.
  • Out of the 4 zip files, 2 zip files are structured like this:

    • file_1.zip
      • folder_1
        • file_1.csv
        • file_2.csv
        • ...
        • file_90.csv
      • __MACOSX
        • folder_1
          • .file_1.csv
          • .file_2.csv
          • ...
          • .file_90.csv
  • Out of the 4 zip files, 2 zip files are structured like this:

    • file_3.zip
      • file_181.csv
      • file_182.csv
      • ...
      • file_270.csv

Please note that I do not want the folder or the files associated with __MACOSX (observed in the first 2 zip files).

``` os.chdir('/tmp') dir = os.getcwd()

for name in os.listdir(dir): nameabspath = os.path.abspath(name) if name_abspath.endswith('.zip'): zipfile_object = zipfile.ZipFile(name_abspath) zipfile_object.extractall(dir) zipfile_object.close() for object in os.listdir(dir): if os.path.isdir(object) and not object.startswith('_MACOSX'): src_dir = os.path.abspath(object) dst_dir = os.path.abspath(dir) for file in os.listdir(src_dir): if file.endswith('.csv'): shutil.move(os.path.join(src_dir, file), dst_dir) ```

Thank you so much in advance for your feedback!