opening and reading a file with file name passed as argument

This is the place for queries that don't fit in any of the other categories.

opening and reading a file with file name passed as argument

Postby UYDeniz » Sat Aug 02, 2014 10:37 am

Hello, This is my first post on this forum. I have started learning python very recently, and I am using the book "Practical Programming" by Gries et al. On page 182, the authors provide a script to open a file called "number_pairs.txt", read the lines that contain two numbers, and then write these numbers and their sum to an output file called "out.txt". Here's the code with the comment of authors excluded:

Code: Select all
def sum_number_pairs(input_file, output_filename):
   with open(output_filename, 'w') as output_file:
      for number_pair in input_file:
         number_pair = number_pair.strip()
         operands = number_pair.split()
         total = float(operands[0]) + float(operands[1])
         new_line = '{0} {1}\n'.format(number_pair, total)
         output_file.write(new_line)


In order for this piece of code to work, we need to call the function like this: sum_number_pairs(open('number_pairs.txt', 'r'), 'out.txt')

What I want to do is writing the function in a way that I can call sum_number_pairs() passing the first argument as number_pairs.txt. However, when I wrote the code below and called sum_number_pairs('number_pairs.txt', 'out.txt'), I get the following error:

File "c:\python\example files\sum_numbers.py", line 6, in sum_number_pairs
operands = number_pair.split()
ValueError: could not convert string to float: 'n'

Here's what I made so far:

Code: Select all
def sum_number_pairs(input_filename, output_filename):
   with open(input_filename, 'r') as input_file:
   with open(output_filename, 'w') as output_file:
      for number_pair in input_file:
         number_pair = number_pair.strip()
         operands = number_pair.split()
         total = float(operands[0]) + float(operands[1])
         new_line = '{0} {1}\n'.format(number_pair, total)
         output_file.write(new_line)


What could I be doing wrong here?

Thanks for any help.
Last edited by Yoriz on Sat Aug 02, 2014 10:55 am, edited 1 time in total.
Reason: First post lock.
UYDeniz
 
Posts: 2
Joined: Sat Aug 02, 2014 9:52 am

Re: opening and reading a file with file name passed as argu

Postby Yoriz » Sat Aug 02, 2014 10:56 am

Welcome to the forum, Please read the new users read this link in my signature.

Apart from the error with your second with statement needing indenting, once that is fixed i dont get the error you have , it works ok.

Your traceback cant be correct the error must be on the line
Code: Select all
total = float(operands[0]) + float(operands[1])

due to you having a character n in your number_pairs.txt which should only have numbers because n cannot be converted to a float.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1175
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: opening and reading a file with file name passed as argu

Postby snippsat » Sat Aug 02, 2014 1:31 pm

Code: Select all
operands = number_pair.split()
print repr(operands) # See all info

You don't need 2 with open statement.
Code: Select all
with open('a') as f,open('b', 'w') as f_out:

a is file to read,and b is file to write to.
User avatar
snippsat
 
Posts: 296
Joined: Thu Feb 21, 2013 12:04 am

Re: opening and reading a file with file name passed as argu

Postby UYDeniz » Sat Aug 02, 2014 3:32 pm

Thanks for both answers. As I have just started to learn Python, I'm not yet fluent in writing robust codes. Trying though. I've just checked if the indentation would solve the problem and it did! And unfortunately I don't have any letters in the number_pairs.txt, if so I could have spent less time on this exercise. Although I find it hard to imagine why the lack of an indentation would raise a value error, I'm hoping to learn in the process.

Best
UYDeniz
 
Posts: 2
Joined: Sat Aug 02, 2014 9:52 am


Return to General Coding Help

Who is online

Users browsing this forum: Dragonization, TakamuraMamoru, Yoriz and 7 guests