
-----------------------------------
Roman
Fri Sep 19, 2008 1:04 am

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

-----------------------------------
Zeroth
Fri Sep 19, 2008 10:57 am

Re: File editing
-----------------------------------

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
Fri Sep 19, 2008 10:47 pm

Re: File editing
-----------------------------------
I can't figure out how to give myself permission :S

file('test.txt', 'w')

Creates the file no problem. But

file('C:\Program Files\test.txt', 'w')

tells me the file/folder doesn't exist.

-----------------------------------
gitoxa
Fri Sep 19, 2008 11:08 pm

RE:File editing
-----------------------------------
That's definitely not the same command Zeroth told you to use. This one works fine:

File = open('C:\Program Files\test.txt','w')

-----------------------------------
Roman
Fri Sep 19, 2008 11:38 pm

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
Sat Sep 20, 2008 8:42 am

RE:File editing
-----------------------------------
Check to see if the file exists.  If it doesn't, create it.

-----------------------------------
Zeroth
Sat Sep 20, 2008 10:31 am

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
Sat Sep 20, 2008 1:07 pm

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 \.

That's what I thought the problem was initially, but I could use '\' or '\\' interchangeably in the command line.

-----------------------------------
Roman
Sat Sep 20, 2008 7:20 pm

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
Sat Sep 20, 2008 11:53 pm

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
Sun Sep 21, 2008 11:58 pm

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
Mon Sep 22, 2008 10:05 am

Re: File editing
-----------------------------------
Update: Some progress, but I'm more confused now.

In the command shell:


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
Mon Sep 22, 2008 7:58 pm

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
Tue Sep 23, 2008 1:15 am

RE:File editing
-----------------------------------
Or you can just use forward slashes.
