Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie - Directory/File Creation

Reply
Thread Tools

Newbie - Directory/File Creation

 
 
Michael J Whitmore
Guest
Posts: n/a
 
      07-08-2003
If I do the following, a file is created in the current working
directory:
TestFile = open("TestTest.out", 'w')

My question is how to create a file that includes a pathname without
having to mkdir beforehand.
Example:
TestFile = open("TestDir\TestTest.out", 'w')

Shouldn't open be smart enough to create the TestDir directory before
creating TestTest.out ?

Is there another command that will do this?
 
Reply With Quote
 
 
 
 
holger krekel
Guest
Posts: n/a
 
      07-08-2003
Peter Hansen wrote:
> Michael J Whitmore wrote:
> >
> > If I do the following, a file is created in the current working
> > directory:
> > TestFile = open("TestTest.out", 'w')
> >
> > My question is how to create a file that includes a pathname without
> > having to mkdir beforehand.
> > Example:
> > TestFile = open("TestDir\TestTest.out", 'w')
> >
> > Shouldn't open be smart enough to create the TestDir directory before
> > creating TestTest.out ?
> >
> > Is there another command that will do this?

>
> Use os.path.makedirs() first, although if you really want to do this,


Peter means os.makedirs() of course.

> I would separate the "ensure this directory exists" part from the
> "create this file" part. Or combine the two, but call your own method
> which internally checks whether the path exists (os.path.split() and
> os.path.isdir() are good for part of this), then creates it if necessary
> using makedirs(), then finally creates the file and returns the
> file object.


Michael might be interested in the following "makepath" recipe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/117243

cheers,

holger

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie Q: conditional object creation Paul Van Delst Ruby 5 07-06-2006 02:05 PM
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Reset network creation counter J.H. Holliday Wireless Networking 4 06-11-2005 08:09 PM
Wizard disk creation problems =?Utf-8?B?U21pdGhlcmVlbg==?= Wireless Networking 3 04-23-2005 03:40 PM
New operator creation Grigorios Angelis VHDL 6 03-02-2004 12:47 AM



Advertisments