LinuxQuestions.org
Visit Jeremy's Blog.
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 11-16-2008, 07:37 AM   #1
Mohtek
Member
 
Registered: Dec 2004
Location: Colorado
Distribution: Kubuntu/Debian/Centos
Posts: 126

Rep: Reputation: 15
Python skip clause for no file in fileinput


Hello, I have a tricky piece of newbie code. I want to have an optional file loaded into a database if the file was given as an option, or alternatively, get the user input for a data file.I have looked at the help(fileinput.input) and it stated that:

This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty.


Here is the relevant snippit of code:

if fileinput.input()!=None:
for line in fileinput.input():
value=string.rstrip(line,"\n")
value=string.split(line,",")
#blow is the db portion
new_user=User(value[0],value[1],value[2])
session.add(new_user)
session.commit()
else:
print "no external data list selected, do you want to add one now?"

It seems to be waiting for something....stdin I assume. Is there any way of doing this through fileinput or should I use the myfile=open("datasheet.txt",'r')....?

Thank you for your input!
 
Old 11-16-2008, 09:34 AM   #2
jtison
LQ Newbie
 
Registered: Dec 2005
Location: Connecticut, USA
Distribution: Fedora Cores 9, 10, 15, 17; SLES 10 & 11; RHEL 6.x
Posts: 19

Rep: Reputation: 0
Always TRUE

Quote:
Originally Posted by mohtech View Post
It seems to be waiting for something....stdin I assume. Is there any way of doing this through fileinput or should I use the myfile=open("datasheet.txt",'r')....?
What was on the command line? Filenames or nothing? My guess is the latter.

Where I think you might be lead astray a bit is that fileinput.input() returns an instance of class FileInput, not necessarily data. Your "if" clause will always return TRUE, regardless of whether there's data there or not.

If your cmdline was bare, FileInput.input() did exactly what it said it would do; waited on stdin. Try feeding your as-is test program a filename or two on the cmdline. See if it runs. It probably will (unless I'm completely off-base).

I'm not quite sure what to tell you about your design, I don't use FileInput very much at all (old schooler). I don't particularly like this class design -- there's no indicator that anything is READY for input, only if you have ALREADY read something. *grumble, mumble, kick desk*

At the scratch-the-surface level, I think I'd replace that leading "if" statement, then just iterate over sys.argv[1:] until I find a readable file. If I find one, then I execute your following "for" clause. I have no idea what FileInput.input() does when it encounters list tuples like these: ['valid.filename', 'not_a_valid_filename', 'another_valid_filename'] ... I just don't use this class myself. Also, if you don't ever want to read stdin, you've got to somehow pre-filter out '-' tokens. Yick.

Of course, while I'm iterating over those args, there's nothing stopping me from calling open(sys.argv[i]) myself, which is exactly what I'd do. Sometimes trying to use a "convenience" class isn't very convenient :-)
 
Old 11-16-2008, 09:52 AM   #3
Mohtek
Member
 
Registered: Dec 2004
Location: Colorado
Distribution: Kubuntu/Debian/Centos
Posts: 126

Original Poster
Rep: Reputation: 15
Talking I agree 100%

It would be far easier to just use the open function to open the files for processing.

At work, I have had to troubleshoot the crockpot a couple of times. Yes, I do mean "troubleshoot the crockpot" so while I am learning python, I am thinking of my potential clientele, for which I want to design software for.

Anyway, this was a fun "what if"

In my life, I am learning how to be lazy through Python. I guess there is a such thing as being too lazy


Thank You!
 
Old 11-16-2008, 11:07 AM   #4
jtison
LQ Newbie
 
Registered: Dec 2005
Location: Connecticut, USA
Distribution: Fedora Cores 9, 10, 15, 17; SLES 10 & 11; RHEL 6.x
Posts: 19

Rep: Reputation: 0
Laziness -- not necessity -- is the mother of invention; which sounds to me exactly how FileInput got invented in the first place

Cheers.
 
  


Reply



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
Using python to edit a python file. deathalele Programming 2 08-27-2008 12:34 PM
Python File#readline skips to end of file taylor_venable Programming 3 05-09-2008 09:32 AM
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
LXer: The Viral Clause LXer Syndicated Linux News 0 07-08-2007 07:46 PM
MySQL: Subquery in HAVING clause ugenn Programming 2 08-11-2002 11:49 AM

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

All times are GMT -5. The time now is 07:17 AM.

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