0

I have a list of date in datetime format. This list has a range date between datetime(2018, 1, 1) to datetime(2020, 12, 31).

I iterate through this list using the following code:

 for day in days:
       # code snippet to get data from each day

My doubt is: How can use os.mkdir() inside my loop to create a folder structure like below

    data
       2018
          Jan
            2018-01-01.csv
            2018-01-02.csv
            ...
          Feb
            2018-02-01.csv
            2018-02-02.csv
            ...
          ...
          ...
      2019
         Jan
           ...
         Feb
           ...
         ...
      2020
         Jan
            ...
         Feb
            ...
         ...

1 Answer 1

1

You can create multiple folders with os.makedirs, it also can ignore if that folder already exists.

for day in days:
    path = day.strftime("%Y/%b")
    os.makedirs(path, exist_ok=True)
    # write your file to path/filename

Or for example:

for day in days:
    path = day.strftime(os.path.join("%Y", "%b", "%Y-%m-%d.csv"))
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w") as file:
        file.write("Your Data Here")

Possible placeholders for strftime can be found on this handy site. You might find it useful to change the intermediate folder names, as the month names won't work particularily well with alphabetical order, so you might want to change that to %m %b such that the preceding number allows for better sorting.

Sign up to request clarification or add additional context in comments.

4 Comments

Awsome solution! A last doubt, if I want to write a pandas dataframe with to_csv method,this method should be call inside with open(path, "w") as file?
@OdiumPura the to_csv can directly accept a path and write to it, in that case you can omit the with-block and directly go for your_df.to_csv(path) (Following pandas.pydata.org/docs/reference/api/…)
I'm getting permission error - denied access when I try to write file to path using df.to_csv(path). But the folder structure works correctly!
I used df.to_csv(path + "/" + filename). Now it's working correctly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.