469,926 Members | 2,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

noobie mkdir problem/question

Hi,

I'm trying to write a script that will create a new directory and then
write the results to this newly created directory but it doesn't seem
to work for me and I don't know why. I'm hoping someone can see my
mistake or at least point me in the right direction.

I start like this capturing the root directory and making my new
"xrefs" directory (I can see the new folder in windows explorer):

root = raw_input("Enter the path where the program should run: ")

xrefs = os.path.join(root,'xrefs')

if (os.path.isdir(xrefs) == 0):
os.mkdir(xrefs)
else:
sys.exit('LOG folder already exists. Exiting program.')

....I do everything else...

And then I'm trying to write the results out to xrefs. But instead of
writing to xrefs they're written to the original directory, i.e., root.
and I'm not sure why.

outputFname = given + '.log'
outputFile = open(os.path.join(xrefs,outputFname), 'w')
outputFile.write(data)
outputFile.close()

Anyone?

Thanks,

Greg

Mar 25 '06 #1
4 1079
if (os.path.isdir(xrefs) == 0):
os.mkdir(xrefs)

--------------------------------------------

os.path.isdir(stuff) returns
True or False

Mar 26 '06 #2
I understand that but I'm still puzzled. Is this the reason why I can't
write files to this directory?

The xrefs directory is created the way I expect it would be using mkdir
but I can't seem to write to it. I thought that my results would be
written to the xrefs directory here but they're ending up in the
original folder not the subfolder.

outputFile = open(os.path.join(xrefs,outputFname), 'w')
outputFile.write(data)
outputFile.close()

What am I missing?

jo************@gmail.com wrote:
if (os.path.isdir(xrefs) == 0):
os.mkdir(xrefs)

--------------------------------------------

os.path.isdir(stuff) returns
True or False


Mar 26 '06 #3
First, what version of python are you using? 2.4.2 (and some previous
versions) use file() instead of open(), although open may still work.

also, if your code in the previous post is still using:

outputFname = given + '.log'
outputFile = open(os.path.join(xrefs,outputFname), 'w')
I hope you have 'given' defined somewhere, since it's not in the code
you show.

give this a try:

output = file(xrefs + r'\filenamewhatever', 'w')
output.write(data)
output.close()

Mar 26 '06 #4
Except for the fact that I don't have any idea where "given"
variable comes from. This works perfectly for me. It writes
data into <root>/xrefs/given.log perfectly.

In the future you should cut/paste your code so we can see
enough to help better. Here is my code with a few changes.

import os
import sys
root = raw_input("Enter the path where the program should run: ")
xrefs = os.path.join(root,'xrefs')

if not os.path.exists(xrefs):
os.makedirs(xrefs)

else:
sys.exit('LOG folder already exists. Exiting program.')

outputFname = 'given.log' # You need to change this line
outputFile = open(os.path.join(xrefs,outputFname), 'w')
data="this is a test\n"
outputFile.write(data)
outputFile.close()
-Larry Bates

ProvoWallis wrote:
Hi,

I'm trying to write a script that will create a new directory and then
write the results to this newly created directory but it doesn't seem
to work for me and I don't know why. I'm hoping someone can see my
mistake or at least point me in the right direction.

I start like this capturing the root directory and making my new
"xrefs" directory (I can see the new folder in windows explorer):

root = raw_input("Enter the path where the program should run: ")

xrefs = os.path.join(root,'xrefs')

if (os.path.isdir(xrefs) == 0):
os.mkdir(xrefs)
else:
sys.exit('LOG folder already exists. Exiting program.')

...I do everything else...

And then I'm trying to write the results out to xrefs. But instead of
writing to xrefs they're written to the original directory, i.e., root.
and I'm not sure why.

outputFname = given + '.log'
outputFile = open(os.path.join(xrefs,outputFname), 'w')
outputFile.write(data)
outputFile.close()

Anyone?

Thanks,

Greg

Mar 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Salmo Bytes | last post: by
6 posts views Thread by Aristotelis E. Charalampakis | last post: by
8 posts views Thread by Sue | last post: by
1 post views Thread by joelbyrd | last post: by
5 posts views Thread by eoindeb | last post: by
30 posts views Thread by MikeC | last post: by
8 posts views Thread by vj | last post: by
2 posts views Thread by _q_u_a_m_i_s's | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.