LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-25-2007, 06:37 PM   #1
dmsynck
LQ Newbie
 
Registered: Apr 2005
Location: "Big D" - Dallas, Texas
Distribution: Linux Mint 8 - "Helena"
Posts: 24

Rep: Reputation: 15
Problem with Python code under Linux


Hi all,

I am working on moving a small Python script from Windows to Linux and am running into a problem with one section of the code. Here is the snippet that is giving me problems.

Code:
for x in Dict1.keys():
        return_code = subprocess.call(["/bin/ping -q -c 3",Dict1[x]])
        if return_code == 0:
            f.write(str(roomNo[i]) + ": \t " + str(Dict1[x]) + "\t Pass \n")
            i = i + 1
            num_pass = num_pass + 1
        elif return_code == 1:
            f.write(str(roomNo[i]) + ": \t " + str(Dict1[x]) + "\t Fail \n")
            i = i + 1
            num_fail = num_fail + 1
        else:
            pass
This code, modified accordingly, runs just fine under windows. Under Linux, it has a problem with the argument to the subprocess call. Here is the traceback:

Code:
Traceback (most recent call last):
  File "./IPinger_Dev.py", line 323, in ?
    main()
  File "./IPinger_Dev.py", line 304, in main
    testIP()
  File "./IPinger_Dev.py", line 129, in testIP
    return_code = subprocess.call(["/bin/ping -q -c 3",Dict1[x]])
  File "/usr/lib/python2.4/subprocess.py", line 412, in call
    return Popen(*args, **kwargs).wait()
  File "/usr/lib/python2.4/subprocess.py", line 542, in __init__
    errread, errwrite)
  File "/usr/lib/python2.4/subprocess.py", line 975, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Thanks in advance

dmsynck
 
Old 10-25-2007, 10:18 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
separate each item.

Code:
return_code = subprocess.call(["/bin/ping", "-q", "-c", "3",Dict1[x]])
or join them all up into one string and use shell=True

Code:
return_code = subprocess.call(" ".join(["/bin/ping -q -c 3",Dict1[x]]), shell=True)
Read this for more info on the difference:
http://docs.python.org/lib/node529.html

ps:
you don't need the .keys(), this should iterate over the dictionary keys just fine:
Code:
for x in Dict1:

Last edited by angrybanana; 10-25-2007 at 10:25 PM.
 
  


Reply


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
Attribute error in python code digitalbrutus Programming 1 08-15-2006 03:39 PM
What is wrong with this python code ? indian Programming 5 09-22-2005 02:20 PM
converting vb6 code to python mrobertson Programming 2 06-02-2005 01:27 PM
easy python code JoeUser11 Programming 1 01-21-2005 01:40 AM
Trouble Python Code Gerardoj Programming 0 11-30-2003 04:31 PM

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

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