我不确定在什么标题下完全考虑这个问题,编码高尔夫看起来是合适的,如果有点不明确.
我对python有一点了解,但看起来很难"阅读".我看到它的方式,理解可能会完成与以下代码相同:
for i in range(10): if i == 9: print('i equals 9')
Run Code Online (Sandbox Code Playgroud)
这段代码比理解目前的工作方式更容易阅读,但我注意到你不能在一行中有两个':'......这也带给我了...
有什么方法可以让以下示例进入ONE LINE.
try:
if sam[0] != 'harry':
print('hello', sam)
except:
pass
Run Code Online (Sandbox Code Playgroud)
像这样的东西会很棒:
try: if sam[0] != 'harry': print('hellp', sam)
except:pass
Run Code Online (Sandbox Code Playgroud)
但是我又遇到了相互冲突的':'我也很想知道是否有办法运行尝试(或类似的东西),除非,我需要放置除了之外似乎完全没有意义:传入那里.这是一条浪费的线.
谢谢你输入......这里有一个笑脸:D
在测试变量有值时,是否有理由决定使用哪一个try或哪些if结构?
例如,有一个函数返回列表或不返回值.我想在处理之前检查结果.以下哪一项更可取,为什么?
result = function();
if (result):
for r in result:
#process items
Run Code Online (Sandbox Code Playgroud)
要么
result = function();
try:
for r in result:
#process items
except TypeError:
pass;
Run Code Online (Sandbox Code Playgroud)
我希望根据文件是否已经存在而写入文件,只有在文件尚不存在时才写入(实际上,我希望继续尝试文件,直到找到不存在的文件).
以下代码显示了潜在攻击者可以插入符号链接的方式,如本文所述,在文件测试和正在写入的文件之间.如果代码以足够高的权限运行,则可能会覆盖任意文件.
有什么方法可以解决这个问题吗?
import os
import errno
file_to_be_attacked = 'important_file'
with open(file_to_be_attacked, 'w') as f:
f.write('Some important content!\n')
test_file = 'testfile'
try:
with open(test_file) as f: pass
except IOError, e:
# symlink created here
os.symlink(file_to_be_attacked, test_file)
if e.errno != errno.ENOENT:
raise
else:
with open(test_file, 'w') as f:
f.write('Hello, kthxbye!\n')
Run Code Online (Sandbox Code Playgroud) 我创建了一个名为Options的类.它工作正常,但不是没有Python 2.我希望它可以在Python 2和3上工作.问题是:Python 2中不存在FileNotFoundError但是如果我使用IOError它在Python 3中不起作用
版本3.3中已更改:EnvironmentError,IOError,WindowsError,VMSError,socket.error,select.error和mmap.error已合并到OSError中.
我该怎么办?(请不要讨论我对便携性的选择,我有理由.)
这是代码:
#!/usr/bin/python
#-*-coding:utf-8*
#option_controller.py
#Walle Cyril
#25/01/2014
import json
import os
class Options():
"""Options is a class designed to read, add and change informations in a JSON file with a dictionnary in it.
The entire object works even if the file is missing since it re-creates it.
If present it must respect the JSON format: e.g. keys must be strings and so on.
If something corrupted the file, just destroy the file or …Run Code Online (Sandbox Code Playgroud) 在确定文件是否存在时,如何使用try语句避免"竞争条件"?
我问,因为一个高度赞成的答案(更新:它被删除)似乎意味着使用os.path.exists()创造了一个不存在的机会.
给出的例子是:
try:
with open(filename): pass
except IOError:
print 'Oh dear.'
Run Code Online (Sandbox Code Playgroud)
但我不明白如何避免竞争条件相比:
if not os.path.exists(filename):
print 'Oh dear.'
Run Code Online (Sandbox Code Playgroud)
如何调用os.path.exists(filename)允许攻击者对他们不能做的文件做些什么?
我正在尝试使用文件上下文管理器写入尚不存在的文件.
a=open ('C:/c.txt' , 'w')
Run Code Online (Sandbox Code Playgroud)
以上不成功.如果文件已经存在,我将如何创建一个文件?
我需要检查当前目录,看看是否存在带扩展名的文件.我的设置(通常)只有一个带有此扩展名的文件.我需要检查该文件是否存在,如果存在,则运行命令.
但是,它会else多次运行,因为有多个文件具有备用扩展名.它必须仅else在文件不存在时运行,而不是每隔一个文件运行一次.我的代码示例如下.
该目录的结构如下:
dir_________________________________________
\ \ \ \
file.false file.false file.true file.false
Run Code Online (Sandbox Code Playgroud)
当我跑:
import os
for File in os.listdir("."):
if File.endswith(".true"):
print("true")
else:
print("false")
Run Code Online (Sandbox Code Playgroud)
输出是:
false
false
true
false
Run Code Online (Sandbox Code Playgroud)
这个问题是,如果我print("false")用有用的东西替换它,它将运行多次.
编辑: 2年前我问过这个问题,而且它仍然看到非常温和的活动,因此,我想把这个留给其他人:http://book.pythontips.com/en/latest/for_-_else. HTML#else从句
我想检查路径中是否存在名为“输出文件夹”的文件夹
D:\LaptopData\ISIS project\test\d0_63_b4_01_18_ba\00_17_41_41_00_0e
如果名为“输出文件夹”的文件夹不存在,则在那里创建该文件夹。
任何人都可以帮忙提供解决方案吗?