如何从变量中删除路径,只保留文件名和扩展名?
root=tk.Tk()
root.withdraw()
FileName=filedialog.askopenfilenames()
print(Filename)
我只想要例如
namefile.txt
,而不是整个路径,比如/path/to/namefile.txt
。For python3.4+, pathlib
from pathlib import Path
name = Path(Filename).name
askopenfilenames()
, 它返回一个元组。尝试使用 Path(Filename[0]).name
。 - hkennyvfilename = filename.replace('\\', '/') # Turns the Windows filepath format to the Literally everything else format.
filename = filename.split('/')[-1]
os.path.basename()
。 - John Gordon