Python: file write issue
ok, here's the problem. I have a small program that needs to read from a file store it in memory a list, edit the list, then rewrite to the same file.
file example.txt
markup"this", "is", "only", "an", "example"
file code.py
for line in list:
items = line
items2 = items + items
list.write(items2)
```**note: <tab> is an actual tab. it is not printed in the file. i'm using it to show that i'm not closing the function
this should make the file example.txt as such:
```markup"this", "is", "only", "an", "example", "this", "is", "only", "an", "example"```
but i receive, for the line "list.write(item2)"
IOError: Errno9 bad file descriptor
what's wrong? if i add the line "print items" or "print items2" it outputs the correct list data. it just errors when i try to write the data. I couldn't see anything in the manual.
If it helps, i have python 2.6 on a 64bit windows 7
stealth- wrote: I believe it's also better coding etiquette (would that be the word?) to open the file in read mode, read from it, close it, open in write mode (w), and then write to it and close it again.
I agree, that way if you encounter any permission errors or anything, you might not have as large a problem.
stealth- wrote: I believe it's also better coding etiquette (would that be the word?) to open the file in read mode, read from it, close it, open in write mode (w), and then write to it and close it again. Indeed. I just gave the solution that involved the least change to the OP's code. He needs to write his code himself.
thanks. that works now.
I just noticed another, new problem. It reads the entire file as a string, not a list of several strings.
Would it be possible to import a file like:
example.py
markupitems = ("this", "is", "an", "example")
so that it would place the list into memory, and then write to the file adding the new data, so it turns out like:
markupitems = ("this", "is", "an", "example", "this", "is", "an", "example")
whats the basis of splitting them? readlines splits the file into lines. readline reads one(?) line.
so given a file, text_file, as
monkey
cheese
house
lines.readline() gives ali (and monkey next time etc etc) and lines.readlines() gives ["ali", "monkey", "cheese", "house"]
assuming "lines" is an open file handle to text_file
to split at ' ' the whole file then do
lines.read().split()```
this reads it as large string then splits it at spaces (the default) if you want to split at b's then you would use split('b')
That worked.
The reason for needing them split is because I need the process to be able to create a list from the data in the file, but the list it needs to be edited and then written back to the file by the program, based on an input by the user.
It needs to be read and written from/to the file so it can be continued another time, because each time the program is run, it needs an input. It's a bit of a bookkeeping/statistics program.
In other words, I'm using the program to edit a "database" instead of doing it manually, which would require a lot more work.
Thanks. It's done, well, at least it's working.
One last thing. Although it's not needed, how would I make an if/elif statement that compares the item to more than one possiblility. I tried:
<tab>...
elif input == "three" or "four" or "five":
<tab>...
else: ...```
But it only ever executes the "if" statement. and yes, the input is a string.
Again, thanks to all for everything. I'm semi-new to python. I've only ever worked with it as a calculator. So lists, files, and if/else statements are pretty new to me.
I'm more into html, css, php, and MySQL.
FallFromINFINITY wrote: if input == "one" or "two" or "three":
That will always evaluate to true. It's basically saying: if (input == "one") or ("two") or ("three") And clearly "two" and "three" are not false expressions. This will be the case in pretty much every programming language. You need to say: if input == "one" or input=="two" or input=="three"