LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2012, 04:14 PM   #1
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Rep: Reputation: 40
PYTHON - basic question


Hello,
why the following code is not working as i expect it to :
Code:
#!/usr/bin/python

from_file ="file.txt"
in_file = open(from_file)

str = in_file.read()

print "Here should be the output from the file - ", in_file.read()
print "Here should be the output from the STR- ", str
in_file.close()

The output is :
Here should be the output from the file -
Here should be the output from the STR- BLAH

in other words print "". in_file.read() doesn't seem to work. I wonder why?

Last edited by DBabo; 12-03-2012 at 05:10 PM.
 
Old 12-03-2012, 05:07 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
you have to import sys, not just argv from sys.
Code:
import sys
jlinkels
 
Old 12-03-2012, 05:11 PM   #3
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by jlinkels View Post
you have to import sys, not just argv from sys.
Code:
import sys
jlinkels
it doesn't look like it's using it. I took the import completely out ( and updated the code above). Still getting the same behavior.
 
Old 12-03-2012, 05:41 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I'm not sure what you expect to see. You read one line from the file and assign it to STR. You then read the next line from the file and print it out. The output shows that the first line read from the file (and assigned to STR) is "BlAH" and the next line read from the file is empty.

Last edited by dugan; 12-03-2012 at 06:05 PM.
 
1 members found this post helpful.
Old 12-03-2012, 08:44 PM   #5
DBabo
Member
 
Registered: Feb 2003
Distribution: Fedora {latest}
Posts: 568

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by dugan View Post
I'm not sure what you expect to see. You read one line from the file and assign it to STR. You then read the next line from the file and print it out. The output shows that the first line read from the file (and assigned to STR) is "BlAH" and the next line read from the file is empty.
well well i wanted to see the content of the file...
But i think I understand what you meant - i read the file into variable str and the pointer (or whatever it's called in python) is @ the end of the file. So when i read the file again - i get 0 bytes.
ekhh silly me.

Thank you for the tip!
AZ
 
Old 12-04-2012, 11:30 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by DBabo View Post
well well i wanted to see the content of the file... But i think I understand what you meant - i read the file into variable str and the pointer (or whatever it's called in python) is @ the end of the file. So when i read the file again - i get 0 bytes.
You are correct. The read() method reads the entire file, not one line. Silly me too.

Ref: http://docs.python.org/2/library/std...l#file-objects

Last edited by dugan; 12-04-2012 at 11:32 AM.
 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic RSS Feed bash / python code to reformat to text? Xeratul Programming 1 08-15-2012 07:32 AM
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 07:24 AM
basic athentication with python zux Programming 4 01-20-2009 09:51 AM
basic python entry tkinter widget how-to donnied Programming 2 03-30-2008 10:36 AM
Error in basic button response program in Python 2.4 with the Tkinter module jojotx0 Programming 1 05-23-2006 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration