我知道如何断言文件是否存在,但不确定如果文件不存在,最好的方法是什么。我正在使用 pytest。
到目前为止我已经有了这个,但这断言文件是否存在。我想也许可以使用!ornot但不确定执行此操作的标准方法是什么。有点菜鸟。谢谢!
path = os.path.join("cs.log")
assert not path.is_file()
Run Code Online (Sandbox Code Playgroud) 如何过滤掉这个列表,以便我们只留下 yyyy-mm-dd 格式的字符串列表?
2021-11-11
2021-10-01
some_folder
some_other_folder
Run Code Online (Sandbox Code Playgroud)
这样我们最终得到一个像这样的列表:
2021-11-11
2021-10-01
Run Code Online (Sandbox Code Playgroud)
如果列表有前缀怎么办?
root/2021-11-11
root/2021-10-01
user/some_folder
root/some_other_folder
Run Code Online (Sandbox Code Playgroud)
我们希望最终得到:
root/2021-11-11
root/2021-10-01
Run Code Online (Sandbox Code Playgroud) x仍然习惯使用 python 并想知道,如果我想计算非文件的数量,我该如何更改这一行?
teeth = [{"tooth_path": str(x)} for x in p if os.path.isfile(x)]
我仍然想teeth返回当前的内容,我只想计算有多少不是文件。
是否可以仅使用.get()字典中的函数来实现此目的?这是我们能做的最好的事情来缩短这段代码吗?
n_dict是一个Dict类型(大写D)并且NES只是一个列表str
eted_info = {}
for key in n_dict:
if key in NES:
eted_info[key] = n_dict[key]
Run Code Online (Sandbox Code Playgroud)
我只是好奇是否有更好/更 Pythonic 的方式来检索值,就像 C# 那样TryGetValue。
我想在倒数第二个斜杠上分割一个字符串,
/tmp/fold/merge/annots所以如果我有一个像我想要获取/tmp/fold/并返回的字符串merge/annots。
同样,如果我有/tmp/long/dir/fold/merge/annots我想要得到/tmp/long/dir/fold/并且merge/annots
最好的方法是什么?我已经尝试rsplit过split几次但没有得到我想要的
试图找到最新版本的字符串列表。例如,
"01022021_1"
"01032021_1"
"02032021_1"
"02032021_2"
Run Code Online (Sandbox Code Playgroud)
最近的将是最后一个,但是我如何使用正则表达式来获取这些字符串并挑选出最新的日期和版本名称?
所以这些是带有版本号的日期。