Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File editing
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Roman




PostPosted: Fri Sep 19, 2008 1:04 am   Post subject: File editing

Hey.

I'm back to Python-ing on my spare time, and with that comes yet another question.

I want to make a program open a file (create one if it doesn't exist, and open one if it does) and read the information. I can do that with the file() command, though it denies me permission when I try to specify a folder from which to get the file. The main problem is that after reading the file, I want the program to (later) overwrite the information. I'm not quite sure how to make it delete the contents of the file and write in new ones. If there is no way, then I could find a work-around. But there should be...

Thanks in advance
-RZ
Sponsor
Sponsor
Sponsor
sponsor
Zeroth




PostPosted: Fri Sep 19, 2008 10:57 am   Post subject: Re: File editing

Python:

f=open('folder/file', 'r') #opens the file for reading. Make sure you have permission to read the folder
text=f.readlines()        #read every line from the file
f.close()                     #close the file
f = open('folder/file', 'w')#we're opening the file in write mode.
f.write("".join(text))     #In this mode, information is completely erased from the file when you write.
f.close()         
Roman




PostPosted: Fri Sep 19, 2008 10:47 pm   Post subject: Re: File editing

I can't figure out how to give myself permission :S

code:
file('test.txt', 'w')


Creates the file no problem. But

code:
file('C:\Program Files\test.txt', 'w')


tells me the file/folder doesn't exist.
gitoxa




PostPosted: Fri Sep 19, 2008 11:08 pm   Post subject: RE:File editing

That's definitely not the same command Zeroth told you to use. This one works fine:

code:
File = open('C:\Program Files\test.txt','w')
Roman




PostPosted: Fri Sep 19, 2008 11:38 pm   Post subject: RE:File editing

Sorry, I kind of overlooked it :S The reference site I was looking at uses the two commands interchangeably, so I got a little confused.

Also, the same error message comes up regardless of the command I'm using. I tried writing a separate program and launching it from shell, with identical results.

What I want to do is CREATE a file in a specific location. Just in case it makes a difference. I guess I'll have to stay up late again to try to get this to work =D
wtd




PostPosted: Sat Sep 20, 2008 8:42 am   Post subject: RE:File editing

Check to see if the file exists. If it doesn't, create it.
Zeroth




PostPosted: Sat Sep 20, 2008 10:31 am   Post subject: Re: File editing

Hrmm... well, I think the issue is because you are using a single \ in the path, which creates an escape character. So, you need to do \\ instead of \.
gitoxa




PostPosted: Sat Sep 20, 2008 1:07 pm   Post subject: Re: File editing

Zeroth @ Sat Sep 20, 2008 10:31 am wrote:
Hrmm... well, I think the issue is because you are using a single \ in the path, which creates an escape character. So, you need to do \\ instead of \.


That's what I thought the problem was initially, but I could use '\' or '\\' interchangeably in the command line.
Sponsor
Sponsor
Sponsor
sponsor
Roman




PostPosted: Sat Sep 20, 2008 7:20 pm   Post subject: RE:File editing

For me it's quite the other way around. I don't seem to be able to use either '\' or '\\'.

I might not have made it clear that I'm trying to make Python create the file in question, so technically it's true that the file doesn't exist. I tried from both command line and IDE, and it doesn't work.

It creates the file if I simply give the file name, but it doesn't create it when I try to specify the location. I read that the 'w' command is supposed to create the file if it doesn't exist, which is why I'm trying.

-RZ
Zeroth




PostPosted: Sat Sep 20, 2008 11:53 pm   Post subject: Re: File editing

Roman, do you have the privileges to create files in the directory you chose? If not, you will be denied. If you are on a limited user account, then that would be why.
Roman




PostPosted: Sun Sep 21, 2008 11:58 pm   Post subject: RE:File editing

I'm programming on the Owner account (in Windows XP) so I'd think so. Is it working for you? I can't shake the feeling that I'm doing something wrong, yet everything seems to be in order. Except that it doesn't work.
Roman




PostPosted: Mon Sep 22, 2008 10:05 am   Post subject: Re: File editing

Update: Some progress, but I'm more confused now.

In the command shell:

code:

file = open("C:\Program Files\hi.txt", "w")
file.write("My computer is a douche")
file.close()
file2 = open("C:\Program Files\text.txt", "w")
IOError: [Errno 2] No such file or directory: 'C:\\Program Files\text.txt'


:S Neither file existed, but the first one got created, and the second didn't.
Zeroth




PostPosted: Mon Sep 22, 2008 7:58 pm   Post subject: Re: File editing

Like I said, use \\ where you can. \t in a string is a tab character... not allowed for a file name.
wtd




PostPosted: Tue Sep 23, 2008 1:15 am   Post subject: RE:File editing

Or you can just use forward slashes.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: