Help with Python I/O

I need help with a few features of Python's I/O system and code

Page 1 of 1

4 Replies - 2010 Views - Last Post: 02 February 2010 - 04:42 PM Rate Topic: ***** 1 Votes

#1 VulcanDesign  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 06-December 09

Help with Python I/O

Posted 02 February 2010 - 08:19 AM

Hi all,

In this Python code below that I am working on, I am having a problem with file I/O. Specifically: I need help finding how many lines there are in an opened file, and how to go to a specific line number in a file.
Here is what I have so far:
import random
import time
wordlist = open('Wordlist.txt', 'r')
counter = 0
for line in wordlist:
    counter += 1
wordlist.close()
wordlist = open('Wordlist.txt', 'r')
randomline = random.randint(1, counter)
randomword = wordlist.readline(randomline)
data = open('DATA.txt', 'w')
data.write(randomword)
data.close()
wordlist.close()

Basically: the file Wordlist.txt is a list of words, each entered on a separate line, and the file DATA.txt is an empty file. My goal with this program is to:
1) Count how many lines there are in the file Wordlist.txt
2) With that info, generate a random number between 1 and the amount of lines there are in the file
3) Find the line number equal to the random number generated
4) Take that line, and write it to DATA.txt

The I figured out how to count the number of lines, but as you can see, it is messy and I was wondering if there is a better way to do it that does not involve opening and closing then opening the file again.

But mainly, I need something like
Readline([[i]size[/i]])
but where you could enter a line number instead of a size in the parens.

Any help given would be extremely...helpful....

Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Help with Python I/O

#2 Perl and Python  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 01-February 10

Re: Help with Python I/O

Posted 02 February 2010 - 08:29 AM

This may help you. I did a similar thing a few days ago for CSC class.

#CSC 120-01 Intro to Programming Methodology   Spring 2010
#Programming Project 2
#Due Thursday, January 28, 2010
#Write a Python programming that reads a  data file named "project2.dat"   which contains a certain number of data lines all of which contain only integers. The program then creates an output file name "project2.out"
#which contains the following items:
#     (1) the total number, Nbr_L, of data lines in the data file;            
#     (2) the total number, Nbr_P, of positive integers in the data file and their sum, Sum_P;
#     (3) the total number, Nbr_Z, of zeroes in the data file; 
#     (4) the total number, Nbr_N, of negative integers in the data file and their sum, Sum_N;
#     (5) The same output to the disk file is also displayed on the Python Shell, followed by the message:  
#                          ........... Normal End of Job .............

import string
infile = open("project2.dat", "r")
outfile = open("project2.out", "w")

Nbr_L = 0
Nbr_P = 0
Sum_P = 0
Nbr_Z = 0
Nbr_N = 0
Sum_N = 0
Total = 0

a = infile.readline()
while a:
    Nbr_L = Nbr_L + 1
    b = string.split(a)  # split the line into a list of words
    c = map(int, b)  # map the list b to a list of integers
    Total += len(c) # add the count of numbers in list c to total count of nbrs

    for p in c:
        if p > 0:
            Sum_P += p
            Nbr_P = Nbr_P + 1

    for n in c:
        if n < 0:
            Sum_N += n
            Nbr_N = Nbr_N + 1

    for z in c:
        if z == 0:
            Nbr_Z = Nbr_Z + 1
      
    a = infile.readline()
    
outfile.write("\nThere are %d data line(s) in the data file\n" % Nbr_L)
outfile.write("\nThere are %d positive integers in the data file\n" % Nbr_P)
outfile.write("\nThe sum of these positive integers is %d\n" % Sum_P)
outfile.write("\nThere are a total of %d zeros in the data file\n" % Nbr_Z)
outfile.write("\nThere are %d negative integers in the data file\n" % Nbr_N)
outfile.write("\nThe sum of these negative integers is %d\n" % Sum_N)

print "There are a total of", Nbr_L, "data lines in the data file."    
print "There are", Nbr_P, "positive integers in the data file."
print "The sum of these positive integers is", Sum_P
print "There are a total of", Nbr_Z, "zeros in the data file."
print "There are", Nbr_N, "negative integers in the data file."
print "The sum of these negative integers is", Sum_N

infile.close()
outfile.close()
print "\n............... Normal End of Job .................\n"


Was This Post Helpful? 1
  • +
  • -

#3 VulcanDesign  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 06-December 09

Re: Help with Python I/O

Posted 02 February 2010 - 08:35 AM

View PostPerl and Python, on 02 February 2010 - 04:29 PM, said:

This may help you. I did a similar thing a few days ago for CSC class.

Thanks! That wasn't exactly what I needed, but it gave me an idea. Imna try it out now...
Was This Post Helpful? 0
  • +
  • -

#4 VulcanDesign  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 06-December 09

Re: Help with Python I/O

Posted 02 February 2010 - 08:41 AM

YES! It works. Thanks for the help.
I just added a new counter that started at zero, and advanced line by line adding one to the counter until the numerical value of the counter reached the randomly generated number. A little roundabout, but it works! Here is the code below:
import random
import time
wordlist = open('Wordlist.txt', 'r')
amountcounter = 0
for line in wordlist:
    amountcounter += 1
wordlist.close()
wordlist = open('Wordlist.txt', 'r')
randomline = random.randint(1, amountcounter)
linecounter = 0
while linecounter < randomline:
    wordlist.readline()
    linecounter += 1
randomword = wordlist.readline()
data = open('DATA.txt', 'w')
data.write(randomword)
data.close()
wordlist.close()


Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Help with Python I/O

Posted 02 February 2010 - 04:42 PM

While your script works, you've sort of done it in a roundabout way. If all you need to do is get a random line from the file, you could do it much simpler:
import random
f = open('Worldlist.txt')
lines = f.readlines()
line_to_write = random.choice(lines)



This way, you only read the file in once, and you don't have to worry about accidentally getting a number that's out of the range of available lines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1