Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Python: file write issue


ghost's Avatar
0 0

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

ynori7's Avatar
Future Emperor of Earth
0 0

It says you have a bad file descriptor because you didn't specify a mode when opening the file. If you don't specify, it defaults to 'r' (read). Try this: markupwith open("c:/example.txt", 'r+') as list: That should allow both read and write.


stealth-'s Avatar
Ninja Extreme
0 0

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.


reaper4334's Avatar
Member
0 0

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.


ynori7's Avatar
Future Emperor of Earth
0 0

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.


ghost's Avatar
0 0

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")


ghost's Avatar
0 0

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')

ghost's Avatar
0 0

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.

ynori7's Avatar
Future Emperor of Earth
0 0

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"


ghost's Avatar
0 0

Awesome. thanks. that did it.

One last time, Thanks to all of you. this is gonna make finishing this a lot easier.


stealth-'s Avatar
Ninja Extreme
0 0

You may want to also look at the self python module for databases. It writes data in very basic databases that are easy to read and write to.