Reputation: 6745
Hi I am currently a beginner to the python language, it is also my first language too. I need some help I am finding it difficult to know what to use to generate permanent directories sub directories and files, for eg; I want the path to generate whatever path i enter if the directories etc. don't exist, i want them created, so I enter C:\user\python\directory\sub-directory\file, then i cant workout what i should import to do the following job.
I am using Python 3.2, any advice?
Upvotes: 0
Views: 103
Reputation: 5707
f = open("c:\file\path","w")
f.write("content of file")
First, you open the file, storing it in variable f
.
You then write to it, using f.write()
Python will create the file and path if it does not exist, I think. (I am sure I've done this before, but I can't remember)
When you have finished using the file, you should use
f.close()
to close the file safely.
Upvotes: 0
Reputation: 115
You can do:
import os
os.makedirs('a/b/c', exist_ok=True)
http://docs.python.org/py3k/library/os.html
Upvotes: 3