PDA

View Full Version : python question - files



Mr.popo
February 3rd, 2008, 10:45 PM
If I had:



inp = open("text.txt", "w")
inp.write("Hello")
inp.close()
inp = open("text.txt", "r")
for line in inp:
print line
inp.close()


If the user then inputted where they would want the file to be saved to then what function would I use?

I was thinking of using the os.system() command to move the file to the location the user specified.

Thanks.

LaRoza
February 3rd, 2008, 11:05 PM
Look at the os module. When you open the file for write access, you are saving it. So, have user specify the file to open.

Martin Witte
February 3rd, 2008, 11:06 PM
Why not asking for a full path, and then if it is valid then use that?


import os.path
filename = raw_input('give filename\n')
if os.path.lexists(os.path.dirname(filename)):
inp = open(filename, "w")
inp.write("Hello")
inp.close()
inp = open(filename, "r")
for line in inp:
print line
inp.close()
else:
print '%s not a valid directory' % os.path.dirname(filename)

Mr.popo
February 4th, 2008, 08:04 PM
Thanks,