LinuxQuestions.org
Review your favorite Linux distribution.
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 01-07-2009, 05:35 AM   #1
agrestic
Member
 
Registered: Jan 2009
Location: atlanta, ga, usa
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61

Rep: Reputation: 16
python noob: arg1 "must be string not tuple"?


Code:
#!/usr/bin/python3
# Filename: vids.py

def watchVideo():
	import subprocess, os
	subprocess.call("ls $HOME/Videos", shell=True)
	movie = str(input('Copy & paste a video; hit Enter : '))
	var = "totem", movie
	os.system(var)

watchVideo()
I run it, input the filename, hit enter. And get the error:
Code:
Traceback (most recent call last):
  File "vids.py", line 11, in <module>
    watchVideo()
  File "vids.py", line 9, in watchVideo
    os.system(var)
TypeError: system() argument 1 must be string, not tuple
I'm new to both Bash and python (also ipython), & I'm using geany. I found this post a few hours earlier,https://www.linuxquestions.org/quest...uments-297277/ then learned about python's "import subprocess, os," etc. So this is my version of what randomx did. But if I try:
Code:
var = "totem" + movie
like randomx did, I get:
sh: totemtommy_emmanuel.mpg: not found
If I'm trying to do things with python a novice shouldn't consider, could anyone point me in the right direction? My current self-teaching list consists of the following:
1. http://wiki.python.org/moin/Beginner...NonProgrammers
2. http://www.redhatmagazine.com/2008/0...l-kept-secret/
3. http://docs.python.org/tutorial/
I haven't started #3 yet.

Edit: In Bash I would just do
Code:
PS3='>>> Type the # of a video & hit Enter: '

select video in $(ls *.mpg)
do
    exec totem $video
    break
done

exit 0
Thanks in advance.

Last edited by agrestic; 01-07-2009 at 09:00 AM. Reason: Added bash example.
 
Old 01-07-2009, 12:56 PM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Try putting a space in your program to add to the command line line like this :
Code:
	var = "totem " + movie
then run the code.

Haven't tested this but an initial look gives me this impression.
 
Old 01-07-2009, 04:08 PM   #3
agrestic
Member
 
Registered: Jan 2009
Location: atlanta, ga, usa
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61

Original Poster
Rep: Reputation: 16
Oh...my...god.
Well thank you kindly.
Pray tell, why did that 1 additional space after the program name make such a difference?
 
Old 01-07-2009, 04:29 PM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Pray tell, why did that 1 additional space after the program name make such a difference?
Well the os.system(var) line is calling the operating system to execute the string that is the contents of var.

For expample - if var="ls -al" the the system call would have been like entering "ls -al" at the keyboard. Without the space the two strings would have no gap.

So in your example:
Code:
 
  var="totem" + "tommy_emmanuel.mpg"
# var now equals "totemtommy_emmanuel.mpg"
  os.system(var)
# tries to execute "totemtommy_emmanuel.mpg" - which will fail.
or :
Code:
 
  var="totem " + "tommy_emmanuel.mpg"
# var now equals "totem tommy_emmanuel.mpg"
  os.system(var)
# tries to execute "totem tommy_emmanuel.mpg" - which will succeed.
I didn't word that very well but I trust you get the idea..
 
Old 01-07-2009, 04:46 PM   #5
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
Quick point, use 'raw_input' as opposed to 'input', try a more c string concat too, easier to maintain on larger strings, so is always good practice. Also, don't call modules in a method- once again its easier to maintain calling at top.

Try os.popen if you're using input and the os module; a lot more secure.

Give diveintopython.org a shot, a very, very good tutorial. With the above in mind, try

Code:
#!/usr/bin/env python

import os

def play_video():
    print os.popen("ls ~/video").read()
    var = raw_input("Please enter video\n>")
    try:
        os.popen("mplayer %s" % (var) )
    except:
        print "Some error occurred"
        raise SystemExit

if __name__ == '__main__':
    play_video()
 
Old 01-07-2009, 06:03 PM   #6
agrestic
Member
 
Registered: Jan 2009
Location: atlanta, ga, usa
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by bgeddy View Post
os.system(var)
# tries to execute "totemtommy_emmanuel.mpg" - which will fail.

...I trust you get the idea..
I spent about an hour looking up irrelevant python-related info &, shockingly , came up with zilch. A plain English answer is refreshing, so thanks.

To jamesc, who said:
"Give diveintopython.org a shot, a very, very good tutorial."

Wilco, thanks; that site's new to me.

Ya know, I sucked at math in high school so programming's gonna be a real doozy. The most abstract thing I do is like, eh, distance divided into the difference in elevation is percent of slope, multiplied by the cosine of the zenith gives linear horizontal distance. As a surveyor, the only kind of math I understand is the kind where I can see the result, e.g., if I do a horizontal resection between deltas it creates a point to occupy & I physically stand there, so it's tangible.
But objects, whizbangs, tuples, jabberwockies, classes and doo-dads are hard as, well, learning a new language.
Wish me luck!
And thanks again.
 
  


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
printing a string that unfolds with Python xadrith Programming 4 07-23-2008 01:13 PM
Extract a number from a complicated string in python horacioemilio Programming 2 12-25-2007 09:09 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
python: full string isn't making it through socket cs-cam Programming 1 11-26-2005 11:20 PM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM

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

All times are GMT -5. The time now is 02:04 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