0

I want to create a directory and then create some files in that directory.

I have already created a directory like so:

if not os.path.exists("output"):
    os.mkdir("output")

How can I now access this directory and create some files using something like this open("foo.txt", "w")?

Vuudi
  • 84
  • 5
  • If you want to _change directory_ (go inside your newly create `output`) and create your files: [how do I cd in python](https://stackoverflow.com/questions/431684/how-do-i-change-directory-cd-in-python) – Brandt Jul 17 '19 at 11:49

1 Answers1

1

Prepend the directory name to your filename:

with open('output/foo.txt', 'w') as f:
    f.write('some content')
abdusco
  • 9,700
  • 2
  • 27
  • 44