Bog*_*ota 3 python file delete-file
我有几个文本文件,名为:
temp1.txt
temp2.txt
temp3.txt
temp4.txt
track.txt
Run Code Online (Sandbox Code Playgroud)
我只想删除以 开头temp和结尾的文件.txt。我尝试使用os.remove("temp*.txt")但出现错误:
The filename, directory name, or volume label syntax is incorrect: 'temp*.txt'
Run Code Online (Sandbox Code Playgroud)
使用 python 3.7 执行此操作的正确方法是什么?
from pathlib import Path
for filename in Path(".").glob("temp*.txt"):
filename.unlink()
Run Code Online (Sandbox Code Playgroud)