How do I get the path and name of the file that is currently executing?

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. For example, let's say I have three files. Using execfile: script_1.py calls script_2.py . In turn, script_2.py calls script_3.py . How can I get the file name and path of script_3.py , from code within script_3.py , without having to pass that informatio

我如何获得当前正在执行的文件的路径和名称?

我有脚本调用其他脚本文件,但我需要获取当前在该进程中运行的文件的文件路径。 例如,假设我有三个文件。 使用execfile: script_1.py调用script_2.py 。 反过来, script_2.py调用script_3.py 。 我怎样才能获得的文件名和路径script_3.py ,从代码中script_3.py ,而不必传递信息,从参数script_2.py ? (执行os.getcwd()将返回原始的脚本文件路径,而不是当前文件。) p1.py: execfile("p2.py") p2.py: im

Python 2 and 3 in Powershell

I have both versions on my PC because I'm working through different tutorials, (I'm still a noob). I've seen a couple of similar questions on here, but nothing specific to Powershell. When I run python in Powershell it brings up 2.7, but how do I specify that I want Python 3? Are there other issues that I need to be aware of? For example when running scripts from notepad++? The

PowerShell中的Python 2和3

我在PC上都有两个版本,因为我正在通过不同的教程进行工作,(我仍然是一个noob)。 我在这里看到了一些类似的问题,但没有特定于Powershell。 当我在Powershell中运行python时 ,它会出现2.7,但是如何指定我需要Python 3? 我还需要了解其他问题吗? 例如,当从记事本++运行脚本? 命令“py”(如果v2是默认值)或“py -2”应该启动Python 2.7,“py -3”应该启动Python 3.请参阅这里以获取详细信息。 我也发现这篇文章。

check key is present in python dictionary

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers How to check if a key exists in an inner dictionary inside a dictionary in python? 3 answers if 'node2' in data: print "node2 Present" else: print "node2Not present" 这是确定密钥是否在字典中的完美适当方式,不幸'node2'是, 'node2'不在你的字典中, ' node2'是

检查密钥存在于Python字典中

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 如何检查一个密钥是否存在于Python中的字典内的内部字典? 3个答案 if 'node2' in data: print "node2 Present" else: print "node2Not present" 这是确定密钥是否在字典中的完美适当方式,不幸'node2'是, 'node2'不在你的字典中, ' node2'是(注意空间): if ' node2' in data: print "node2 Present" e

How to update value of checkbox in db using Flask

This question already has an answer here: Flask isn't getting the checkbox value 1 answer How to return dictionary keys as a list in Python? 9 answers Check if a given key already exists in a dictionary 18 answers you can convert the ImmutableMultiDict to dict todo = dict(request.form).keys[0] and then filter by it, complete and commit.

如何使用Flask更新数据库复选框的值

这个问题在这里已经有了答案: Flask没有得到复选框值1的答案 如何将字典键作为Python中的列表返回? 9个答案 检查给定的密钥是否已经存在于字典中18个答案 你可以将ImmutableMultiDict转换为字典 todo = dict(request.form).keys[0] 然后过滤它,完成并提交。

python: override a single dict key based on a separate dict

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers Just try to replace it. This avoids one unnecessary hashing (compared to using if 'day' in query: ) or looping over the dictionary and follows Pythons EAFP principle: try: query['day'] = day_mapping[query['day']] except KeyError: pass You may use dict.get(..) to check fo

python:覆盖基于单独字典的单个字典密钥

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 try替换它。 这避免了一次不必要的哈希(与if 'day' in query:使用if'day if 'day' in query:相比)或循环查找字典并遵循Pythons EAFP原则: try: query['day'] = day_mapping[query['day']] except KeyError: pass 您可以使用dict.get(..)检查dict是否存在'day'键,如下所示: query = {'day': 0, 'item'

python2 code get error when using python3.5

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers distutilscross issue when install PyAudio for cross-platform 1 answer how to solve AttributeError: '_Environ' object has no attribute 'has_key' 1 answer has_key is removed in python3, but you shouldn't be using it in 2 either. Use the in operator: if self.cross_co

python2代码在使用python3.5时会出错

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 distutilscross问题,当安装PyAudio的跨平台1的答案 如何解决AttributeError:'_Environ'对象没有属性'has_key'1答案 has_key在python3中被删除,但是你不应该在2中使用它。 使用in运算符: if self.cross_compile and 'PYTHONXCPREFIX' in os.environ: has_key()在Python 3.x中被删除。 使用in或get 'PYTHONXCPREFIX'

Python dictionary failsafe

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers You can use a try/except block: try: # Notice that I got rid of str(Lookup) # raw_input always returns a string print Guide[Lookup] # KeyErrors are generated when you try to access a key in a dict that doesn't exist except KeyError: print 'Key not found.' Also, in order f

Python字典故障安全

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 你可以使用try / except块: try: # Notice that I got rid of str(Lookup) # raw_input always returns a string print Guide[Lookup] # KeyErrors are generated when you try to access a key in a dict that doesn't exist except KeyError: print 'Key not found.' 此外,为了让您的代码正常工作,您需要制作下面这行代码

Python safe dictionary key access

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers Access dict key and return None if doesn't exist 6 answers You missed the canonical method, dict.get() : color_1 = data.get('color') It'll return None if the key is missing. You can set a different default as a second argument: color_2 = dict.get('color', 'red') Check out di

Python安全字典键访问

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 访问字典键并返回None(如果不存在)6个答案 你错过了规范的方法, dict.get() : color_1 = data.get('color') 如果密钥丢失,它将返回None 。 您可以将另一个默认设置为第二个参数: color_2 = dict.get('color', 'red') 查看dict.get() 。 如果在字典中找不到密钥,则可以提供返回值,否则将返回None 。 >>> data = {'color':

Check if key is in the dictionary?

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers Just use the in operator (which is accompanied by the not in operator): if (1, 'a') in edges: if (1, 'a') not in edges: Below is an excerpt from the documentation of Python's dictionary type, where d is a dictionary: key in d Return True if d has a key key , else False . key n

检查密钥是否在字典中?

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 只需使用in运算符(它与not in运算符一起): if (1, 'a') in edges: if (1, 'a') not in edges: 下面是Python的字典类型的文档摘录,其中d是一个字典: key in d 如果d有一个key ,则返回True ,否则返回False 。 key not in d 等同于not key in d 。

how do check if a value exists in python

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers 您可以使用in这样的: if "virtualDisk.totalReadLatency" in counterValues: doSomething() else: pass You can use a try/except block (according to the Zen of Python, it's better to ask for forgiveness than permission ;-) try: # your lookup except KeyError: # your error h

如何检查python中是否存在一个值

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 您可以使用in这样的: if "virtualDisk.totalReadLatency" in counterValues: doSomething() else: pass 你可以使用try / except块(根据Python的Zen,最好是要求原谅而不是权限;-) try: # your lookup except KeyError: # your error handling 通过这种方式,你可以将所有的关键字查找包装成一次try (更好的是将其重构为单