ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61
Rep:
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:
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
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..
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()
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61
Original Poster
Rep:
Quote:
Originally Posted by bgeddy
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.