By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

how to read to and write from a file

P: 9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #1
Share this Question
Share on Google+
13 Replies

P: 9
How do i initialise the r and w command?
Dec 7 '13 #2

Expert 100+
P: 1,043
i started reading here:
http://docs.python.org/2/tutorial/inputoutput.html


and came up with:
Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/temp> cat abcd
  2. a
  3. b
  4. c
  5. d
  6.  
  7. luuk@opensuse:~/temp> cat abcd.py
  8. #!/usr/bin/python
  9.  
  10. f = open('abcd','r')
  11. for line in f:
  12.   print ord(line[0:1])
  13.  
  14. luuk@opensuse:~/temp> ./abcd.py
  15. 97
  16. 98
  17. 99
  18. 100
  19. 10
  20. luuk@opensuse:~/temp>
  21.  
Dec 7 '13 #3

Expert 100+
P: 1,043
but slightly better:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. f = open('abcd','r')
  4. c = True
  5. while c:
  6.         c = f.read(1)
  7.         if c>' ':
  8.                 print ord(c)
Dec 7 '13 #4

P: 9
Hi thanks, i created a text file with the same characters as my input but the code "print ord(line[0:1])" is not working. I know ord is for ascii.
Dec 7 '13 #5

P: 9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #6

Expert 100+
P: 1,043
If your input file contains this:
Expand|Select|Wrap|Line Numbers
  1. 1 34
  2. 12 4
  3. 123 
  4.  
can you tell, what the expected output should be?

Because you are talking about 'product', and as far as my English goes, the product of 2 and 3 is (2x3=) 6
But i only see additions in your 'code-so far'...
Dec 7 '13 #7

P: 9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #8

Expert 100+
P: 1,043
I asked a question, and i did not get an answer.....

Why do you think i asked that question?

Just so you could post your code again, without specifying what kind of error you have? and on what line the error is?

rule1: please use '[code]' tags around your code
rule2: only post code if its different from previous post ;)
rule3: answer a question when asked, how else can you be helped?
rule4: i do not make rules here........

But, to answer your question,.....

Your code gives an error, on what line, and what error?
Expand|Select|Wrap|Line Numbers
  1.     wordlist = line.split()
  2.            ^
  3. IndentationError: expected an indented block
  4.  
It it's not this error, than please use '[code]' tags to post your code..... ;)
Dec 7 '13 #9

P: 9
not outputting the result in another file named b.txt
Dec 7 '13 #10

P: 9
not outputting the result in b.txt
Dec 7 '13 #11

Expert 100+
P: 1,043
maybe because you are only outputting it to screen:
Expand|Select|Wrap|Line Numbers
  1. print("The product is", product)
Dec 7 '13 #12

P: 9
Here is the problem:
y.write(line) outputs the result as characters in b.txt
but i need the result as integer value.
Dec 7 '13 #13

Expert 100+
P: 1,043
"y.write(line) outputs the result as characters in b.txt"
what is y ??
Dec 7 '13 #14

Post your reply

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