Writing numbers into a file I'm taking my first ever CS class and I have an assignment due Friday. I just wanted someone to check my code.

Instructions:


  Write a program that:
  
  
  gets the name of a text file of numbers from the user. Each number in the file is on its own line.
  reads in those numbers one at a time
  writes the even numbers to a file named even.txt
  writes the odd numbers to a file named odd.txt
  displays to the user the sum of the positive numbers and the count of the negative numbers.
  


Here is what I have:

def main():
    #Open text file for reading
    fileName   = input('Enter name of a file of numbers (include the whole path): ')
    numberFile = open(fileName, 'r')

#Priming read
    number = numberFile.readline()

#Setting up loop to continue reading until
#an empty line is reached
    total = 0
    count = 0
    while number != '':
        number = float(number)                  #convert from string to number
        if number%2 == 0:                       
            evenNumber = open('even.txt', 'w')  #writes even numbers into a file
            evenNumber.write(number + '\n')
        else:
            oddNumber  = open('odd.txt', 'w')   #writes odd numbers into a file
            oddNumber.write(number + '\n')

        for number in numberFile:
            number = float(number)      #convert from string to number

            if number &lt;= 0:             #identify negative numbers
                count += 1              #count negative numbers

            if number &gt;= 0:             #identify positive numbers
                total += number         #sum of positive numbers
        number = numberFile.readline()

    numberFile.close()                  #close file after program is complete

    print('The count of the negative numbers is: ', count)
    print('The sum of the positive numbers is: ', total)

main()


Example of a "file of numbers" (each number in a new line):

25
18
-7
4.2
99
103
-100
Some quick comments:


At least as written, your code doesn’t work for me. I created the following file: 

1


and passing it to your script got the following error: 

Enter name of a file of numbers (include the whole path): numbers2.txt
Traceback (most recent call last):
  File "parsenumbers.py", line 37, in &lt;module&gt;
    main()
  File "parsenumbers.py", line 20, in main
    oddNumber.write(number + '\n')
TypeError: unsupported operand type(s) for +: 'float' and 'str'




I’m using Python 3.4.3 on OS X.
I fixed that (fix hidden for you to work it out yourself):


   Cast number to str(number) before writing.


Then I created a longer file: 

1
2
3
4
5


And ran the script again.  Still broken: 

# odd.txt
1.0

# even.txt
(Empty)


It’s also strange that 1 has become 1.0 in the output file. I’d expect it to be carried through unchanged – what’s happened?


Once you’ve got the code working, some style comments:
* Run your code through a PEP 8 linter. This will catch style issues like variable names, whitespace, etc.


Everything is wrapped up in one massive main() function. That makes this code difficult to test and reuse – it would be better if you had separate functions, each of which took a list of numbers, and would do different things.

e.g. one function gets the sum of the negatives, another the positives, another to write the evens to a file.
Rather than calling main() at the top-level, put it in an if __name__ == '__main__' block. This means it only runs if the script is called directly – and other people can import from your script without side effects.
Use with open(filename) as f:, not f = open(filename) … f.close(). This is more idiomatic, and safer – you can’t leave files open accidentally. (You never actually close even.txt or odd.txt after you’ve opened them!)
None of your comments tell me anything I couldn’t get from reading the code.  They should explain why the code is written this way – how does it relate to the problem it’s trying to solve – not what it’s doing.  And as a sidebar, it’s really annoying if comments don’t line up with the code they’re describing.
Use better variable names. You have count and total, but it’s not obvious what these correspond to.  How about negative_count and positive_total – otherwise it sounds like you’re getting a count/total for the entire set.
Your code assumes that zero is negative, and will count it as such. That’s somewhat counter-intuitive: instinct tells me that zero is neither positive nor negative, but there are times when it’s more complicated.

This would be a really good spot for a comment: if it’s not completely ambiguous, state what choice you’re making (in plain English), and why you’ve chosen to do it this way.