LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 05:14 PM   #1
DBabo
Member
 
Registered: Feb 2003
Distribution: Scientific Linux 6, Fedora
Posts: 393

Rep: Reputation: 33
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 06:10 PM.
 
Old 12-03-2012, 06:07 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
you have to import sys, not just argv from sys.
Code:
import sys
jlinkels
 
Old 12-03-2012, 06:11 PM   #3
DBabo
Member
 
Registered: Feb 2003
Distribution: Scientific Linux 6, Fedora
Posts: 393

Original Poster
Rep: Reputation: 33
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, 06:41 PM   #4
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,990

Rep: Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555
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 07:05 PM.
 
1 members found this post helpful.
Old 12-03-2012, 09:44 PM   #5
DBabo
Member
 
Registered: Feb 2003
Distribution: Scientific Linux 6, Fedora
Posts: 393

Original Poster
Rep: Reputation: 33
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, 12:30 PM   #6
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,990

Rep: Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555Reputation: 1555
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 12:32 PM.
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:32 AM
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 08:24 AM
basic athentication with python zux Programming 4 01-20-2009 10:51 AM
basic python entry tkinter widget how-to donnied Programming 2 03-30-2008 11:36 AM
Error in basic button response program in Python 2.4 with the Tkinter module jojotx0 Programming 1 05-23-2006 08:43 PM


All times are GMT -5. The time now is 08: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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration