0

Hi can someone help me to create a right path for my files in python? My path must be exported/files/{t}/{y}/filename. If i put in dir variable {y} on the end it won't work for some reason. My code is

tables=["table1","table2"]
years=["2010","2020"]
months=[1,2,3,4...12]
for t in tables:
     for y in years:
           for m in months:
                dir=(f"exported/files/{t}")
                os.mkdir(dir)
                sqlcode="select * from {t} where some condition..."
                filename= (f"{t}_{y}_{m}.sql")
                with open(os.path.join(dir,filename_),"w") as writer:
                     writer.write(sqlcode)
1
  • 2
    you could use os.makedirs and call it with exist_ok=True Commented Jan 7, 2021 at 10:48

2 Answers 2

1

you can use Path from pathlib :

from pathlib import Path



tables=["table1","table2"]
years=["2010","2020"]
months=[1,2,3,4...12]
for t in tables:
     for y in years:
           for m in months:
                full_path = f"exported/files/{t}"
                Path(full_path ).mkdir(parents=True, exist_ok=True)
                sqlcode="select * from {t} where some condition..."
                filename= (f"{t}_{y}_{m}.sql")
                with open(os.path.join(full_path ,filename),"w") as writer:
                     writer.write(sqlcode)     

Note: avoid using the keyword dir as it is saved keyword.

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

4 Comments

Nice, thank you for your answer. I still don't get it why just putting {y} in the dir variable didn't work.
because there is nested dirs.. its not creating a nested dirs unless specified... the solution I gave you cover many use cases///
Ok one more upgrade question. Lets assume that i will have sql table1 with dates from 2019 till 2020, but i will have also a table table1_2018. How to go through table 1 just for 2019 and 2020 and in table1_2018 just with 2018.
you better open a different question for that, tbh i dont use sql...
1

you should use os.makedirs() method instead of os.mkdir() method beacuse mkdir() can create a single sub-directory but makedirs() used to create branches also put exist_ok=True in os.makedirs() method and remove underscore from filename_ in line 11

should look like this:

tables=["table1","table2"]
years=["2010","2020"]
months=[1,2,3,4,5,6,7,8,9,10,11,12]
for t in tables:
     for y in years:
           for m in months:
                dir=(f"exported/files/{t}")
                os.makedirs(dir, exist_ok=True)
                sqlcode="select * from {t} where some condition..."
                filename= (f"{t}_{y}_{m}.sql")
                with open(os.path.join(dir,filename),"w") as writer:
                     writer.write(sqlcode)

Comments

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.