469,282 Members | 2,000 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Trying to manipulate a list of words in a textfile

3
Hi,

I am quite new to Python, and the following puzzles me, maybe there is someone here who can tell me what is wrong?

I have a list of words in a plain textfile (encoded in latin-1). There is one word per line, and 106299 words in total. I want to load the words to a list because I want to manipulate them, and then write them to a different file...

The mystery for me is that the new file only contains 106211 lines, even though I have not done anything with the list yet, and the last line contains a word that has been truncated.

I have tried many different version of the program, but this is the most basic one, and it gives the error mentioned above:

Expand|Select|Wrap|Line Numbers
  1. f = open("list.txt", "r")
  2. wordlist = [w.lower() for w in f.readlines()]
  3. f.close
  4.  
  5. f = open("new_list.txt", "w")
  6. f.writelines(wordlist)
  7. f.close
  8.  
I am running this from Windows XP in Python 2.5.1, using IDLE.
Jan 2 '08 #1
4 1351
isakb
3
An update: I was just told that IDLE is basically "useless" by a fellow programmer, so I tried running the script directly from the terminal instead of using IDLE. So this seems to be just an IDLE bug? (Sorry for not realizing that the results would differ, I thought IDLE used the same python as the terminal.)
Jan 2 '08 #2
bvdet
2,851 Expert Mod 2GB
An update: I was just told that IDLE is basically "useless" by a fellow programmer, so I tried running the script directly from the terminal instead of using IDLE. So this seems to be just an IDLE bug? (Sorry for not realizing that the results would differ, I thought IDLE used the same python as the terminal.)
The problem is not with IDLE. You did not close the file. You must include '()' when closing the file.
Expand|Select|Wrap|Line Numbers
  1. f = open('file_name')
  2. f.close()
The data is flushed through the buffer when the file is close. All the data was not written in your case.
Jan 2 '08 #3
isakb
3
Oh, thank you for that information. I also tried changing the buffering mode to none, which made it work in IDLE, so your explanation makes much sense. So I understand that you should always close a file after writing to it, but it is not necessary when reading from it?
Jan 2 '08 #4
bvdet
2,851 Expert Mod 2GB
Oh, thank you for that information. I also tried changing the buffering mode to none, which made it work in IDLE, so your explanation makes much sense. So I understand that you should always close a file after writing to it, but it is not necessary when reading from it?
It is always a good idea to close every file that you open. Python takes care of garbage collection, so it is not a critical issue when reading the contents of a file as in:
Expand|Select|Wrap|Line Numbers
  1. data = open('file_name').read()
In this case, no file object is created.
Jan 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Warren Oates | last post: by
3 posts views Thread by Stewart | last post: by
7 posts views Thread by 3KWA | last post: by
1 post views Thread by =?Utf-8?B?UmFm?= | last post: by
1 post views Thread by antar2 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.