当我接受参数时,如何在没有复合条件的情况下检查两个是否同时出现
即
#!/usr/bin/python
import random, string
import mymodule
import sys
z = ' '.join(sys.argv[2:])
q = ''.join(sys.argv[3:])
a = ''.join(sys.argv[2:])
s = ' '.join(sys.argv[1:])
flags = sys.argv[1:5]
commands = [["-r", "reverse string passed next with no quotes needed."], ["-j", "joins arguments passed into string. no quotes needed."], ["--palindrome", "tests whether arguments passed are palindrome or not. collective."],["--rand","passes random string of 10 digits/letters"]]
try:
if "-r" in flags:
if "-j" in flags:
print mymodule.reverse(q)
if not "-j" in flags:
print mymodule.reverse(z) …Run Code Online (Sandbox Code Playgroud) 从docs.python.org引用:
" sys.argv传递给Python脚本的命令行参数列表.argv[0]是脚本名称(它是依赖于操作系统的,无论这是否是完整路径名.)如果命令是使用-c解释器的命令行选项执行的,argv[0]则设置为字符串'-c'.如果没有脚本名称传递给Python解释器,argv[0]则为空字符串."
我错过了什么,或者sys.argv[0]总是返回脚本名称,并且'-c'我必须使用sys.argv[1]?
我在GNU/Linux上使用Python 3.2进行测试.
我经历了" 在Python中记录未捕获的异常 ".而且,我试过这个:
import web
import sys
app = web.application((
'/test', 'test'), globals())
def test_func(e_type, value, traceback):
print "Handled exception here.."
class test:
def GET(self):
a = 1/0
if __name__ == "__main__":
sys.excepthook = test_func
app.run()
Run Code Online (Sandbox Code Playgroud)
在这里,您可以轻松查看是否有GET /test请求,我故意提出ZerDivisionError.正如我已经覆盖sys.excepthook,我希望方法test_func执行ZeroDivisionError.
然而,这段代码并没有按照预期工作.我观察到,当我尝试覆盖excepthook独立代码(不在web-app中)时,它工作正常.正确调用新方法(overriden).
知道为什么这种不同的行为?
终奌站:
python test.py blah='blah'
Run Code Online (Sandbox Code Playgroud)
在test.py中
print sys.argv
['test.py', 'blah=blah'] <------------
Run Code Online (Sandbox Code Playgroud)
怎么能保持它的''或者
有没有办法知道一个arg是用""还是""包裹?
我需要解决 sys.path 提供的分隔符和 os.path.join 使用的分隔符之间的差异。
我模仿了这个 Esri 方法(共享 Python 脚本的技术)来使我的脚本可移植。它目前在 Windows 中使用,但最终将在 Linux 服务器上运行;我需要让 Python 确定适当的斜线。
他们的建议是:
# Get the pathname to this script
scriptPath = sys.path[0]
# Get the pathname to the ToolShare folder
toolSharePath = os.path.dirname(scriptPath)
# Now construct pathname to the ToolData folder
toolDataPath = os.path.join(toolSharePath, "ToolData")
print "ToolData folder: " + toolDataPath
Run Code Online (Sandbox Code Playgroud)
但是这个输出ToolData folder: C:/gis\ToolData——显然混合斜线是行不通的。
这个问题(在 windows 上混合斜线与 os.path.join)包括解决方案的基本方法:
在将其放入
os.path.join. 通过这种方式,您可以确保os.path.join不会基于可能错误的输入做出错误的决定
但是,我不确定如何确保它可以跨平台工作。如果我.replace("/","\\")在sys.path[0]结果上使用,这对 …
没有sys.getlinuxversion(),我觉得我可能需要从几个地方把它拼凑起来(这很好); 但是,也许不是吗?
import sys, os
from collections import namedtuple
## This defines an interface similar to the object that getwindowsversion() returns
## (I say 'similar' so I don't have to explain the descriptor magic behind it)
WindowsVersion = namedtuple('WindowsVersion', 'major minor build platform product_type service_pack service_pack_major service_pack_minor suite_mask')
## Looking for something that quacks like this:
LinuxVersion = namedtuple('LinuxVersion', 'major minor build whatever attrs are needed')
## To create something that quacks similarly to this (in case you're …Run Code Online (Sandbox Code Playgroud) 在下面的代码中它说:"语法警告:名称'颜色'在全局声明全局颜色之前被分配"但是,我在分配它之前声明了全局颜色?我很迷茫.我运行它,它的工作原理,但我只是不明白语法警告指向...
from Tkinter import *
from sys import exit
from random import *
color = "black" #Sets default color to black
w,h=640,480 #Width, height of canvas
root=Tk()
pixelcount = 0 #Sets the inital pixelcount to 1
tool = 1 #Sets deafu
ptX, ptY, ptX2, ptY2 = 0, 0, 0, 0
cnvs=Canvas(root,width=w,height=h,bg='#D2B48C') # 210 180 140
cnvs.pack(side = RIGHT)
buttons = Frame(root, width = 80, height = h) #Creates region for buttons
buttons.pack(side = LEFT) #Put button region on left
def …Run Code Online (Sandbox Code Playgroud) 在Python文档中,据说可以在外层拦截exit调用。那么“在外部层面拦截”实际上是什么意思呢?更具体地说,拦截呼叫是什么意思?
我是python的新手,并试图弄清楚这几个小时..我想在我的脚本中使用os更改工作目录
os.chdir("~") # not working.
os.getcwd #--> "/home/pi/Documents"
#I want to change into a subfolder I tried following
"subfolder"
"subfolder/"
"~../subfolder"
"/subfolder"
Run Code Online (Sandbox Code Playgroud)
试过:
sys.path.append.
Run Code Online (Sandbox Code Playgroud) 假设我有两行这样的代码:
a = [1 , 2 , 3 , 4]
print (a)
Run Code Online (Sandbox Code Playgroud)
现在我想在这段代码中添加一些内容,为用户提供 2 个选项:
1:按“ Enter ”继续。
在这种情况下,代码应该打印“a”
2:按“ Esc ”退出程序。
在这种情况下,应该停止程序(例如退出代码)。
我需要提一下,我只想使用这 2 个键(Enter和Esc)而不是任何键
我一直在玩raw_input和sys.exit但它不起作用。知道在这个例子中我该怎么做吗?谢谢!
python ×10
sys ×10
python-3.x ×2
arcpy ×1
arguments ×1
argv ×1
draw ×1
exit ×1
portability ×1
python-2.7 ×1
tkinter ×1
user-input ×1