Python命令行参数sys.argv无法正常工作

时间:2015-11-24 20:49:49

标签: python command-line-arguments

我在使用sys.argv时遇到了一些麻烦,以便能够从命令行运行程序。我正在运行IDLE 3.5。我试过通过命令提示符和Python(3.5)命令行运行下面的程序。我两个都收到错误:

在Python(3.5)中:

>>> python testing.py
  File "<stdin>", line 1
    python testing.py
                 ^
SyntaxError: invalid syntax
>>>
命令提示符中的

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

testing.py读取:

import sys

def main():
    print sys.argv


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

我观看的教程显示,这个python文件在从命令行执行时只会返回程序名称“testing.py”

我怀疑某些文件目录存在问题,当命令行尝试打开文件时,它就不存在了。

有谁知道如何弄清楚问题是什么?如果是这样,我该如何纠正呢?

很抱歉,如果我的问题形成不良。我是python和stackoverflow的新手,我真的不知道我在做什么。

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

1 个答案:

答案 0 :(得分:1)

看起来你是从python解释器运行的。

您需要在命令行上运行此命令(OS X上的终端,Windows上的命令提示符?)。

SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '15/11/2015' AND '16/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '16/11/2015' AND '17/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '17/11/2015' AND '18/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '18/11/2015' AND '19/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '19/11/2015' AND '20/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '20/11/2015' AND '21/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '21/11/2015' AND '22/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '22/11/2015' AND '23/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '23/11/2015' AND '24/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '24/11/2015' AND '25/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '25/11/2015' AND '26/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '26/11/2015' AND '27/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '27/11/2015' AND '28/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '28/11/2015' AND '29/11/2015'
SELECT USER_ID, ROOM, DATE FROM booking_table WHERE DATE BETWEEN '29/11/2015' AND '30/11/2015'

这意味着您告诉Python执行testing.py脚本。