简体   繁体   中英

Python Command Line Argument sys.argv not working

I'm having some trouble using sys.argv in order to be able to run the program from the command line. I am running IDLE 3.5. I have tried running the program below both through a command prompt and the Python(3.5) command line. On both I am receiving an error:

in Python(3.5):

>>> python testing.py
  File "<stdin>", line 1
    python testing.py
                 ^
SyntaxError: invalid syntax
>>>

in Command Prompt:

C:\Users\Brice_000>python testing.py
python: can't open file 'testing.py': [Errno 2] No such file or directory

testing.py reads:

import sys

def main():
    print sys.argv


if __name__ == "__main__":
main(sys.argv)

The tutorial I watched shows that this python file, when executed from the command line will return just the name of the program "testing.py"

I have a suspicion that somehow there's an issue with the file directories and when the command line is trying to open the file, it isn't there.

Does anyone know how I might go about figuring out what the issue is? And if so, how can I correct it?

Sorry if my question here is poorly formed. I'm new to python and stackoverflow and I don't really know what I'm doing yet.

Output of dir:

C:\Users\Brice_000>dir
 Volume in drive C has no label.
 Volume Serial Number is 60E5-73E9

 Directory of C:\Users\Brice_000

11/01/2015  04:45 PM    <DIR>          .
11/01/2015  04:45 PM    <DIR>          ..
11/01/2015  04:47 PM    <DIR>          .gimp-2.8
11/01/2015  05:07 PM    <DIR>          .idlerc
06/21/2015  09:20 PM    <DIR>          .jagex_cache_32
08/23/2015  08:00 PM    <DIR>          .oracle_jre_usage
01/11/2013  03:59 PM    <DIR>          .swt
11/01/2015  04:45 PM    <DIR>          .thumbnails
09/15/2015  10:12 PM    <DIR>          3D Objects
10/14/2015  08:48 AM    <DIR>          Contacts
08/29/2015  04:28 PM    <DIR>          Creative Cloud Files
11/16/2015  05:08 PM    <DIR>          Desktop
11/20/2015  11:32 PM    <DIR>          Documents
11/24/2015  02:26 PM    <DIR>          Downloads
04/08/2013  10:26 PM    <DIR>          Dropbox
01/12/2013  01:16 PM    <DIR>          dumps
10/14/2015  08:48 AM    <DIR>          Favorites
06/21/2015  09:37 PM                23 jagexappletviewer.preferences
02/26/2013  06:31 PM    <DIR>          jagexcache
06/21/2015  09:21 PM    <DIR>          jagexcache1
02/19/2013  07:45 PM                50 jagex_cl_loginapplet_LIVE.dat
05/13/2014  10:34 PM                48 jagex_cl_oldschool_LIVE.dat
06/21/2015  09:21 PM                48 jagex_cl_runescape_LIVE.dat
06/21/2015  09:21 PM                49 jagex_cl_runescape_LIVE1.dat
10/14/2015  08:48 AM    <DIR>          Links
10/20/2015  05:30 AM    <DIR>          Music
11/05/2015  12:42 AM    <DIR>          OneDrive
10/28/2015  08:50 PM    <DIR>          Pictures
06/22/2015  12:32 AM                24 random.dat
10/14/2015  08:48 AM    <DIR>          Saved Games
10/14/2015  08:48 AM    <DIR>          Searches
08/04/2015  08:35 PM    <DIR>          SkyDrive
02/25/2014  03:54 AM    <DIR>          SkyDrive (2).old
04/07/2014  01:55 PM    <DIR>          SkyDrive (3).old
02/16/2014  02:50 PM    <DIR>          SkyDrive.old
03/12/2015  08:15 PM    <DIR>          Tracing
10/14/2015  08:48 AM    <DIR>          Videos
               6 File(s)            242 bytes
              31 Dir(s)  49,005,813,760 bytes free

Looks like you're running from a python interpreter.

You need to run this on the command line (Terminal on OS X, Command Prompt? on Windows).

# terminal or command prompt!
python testing.py

This means that you tell Python to execute the testing.py script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM