[英]Finding file knowing only file name, not path - Python
有沒有什么方法可以找到只知道文件名的文件(只有文件名沒有文件路徑),例如“20.10.2021.txt”並將這個文件復制到其他目錄?
如果您不確切知道路徑,則可能有多個匹配文件名的候選者:
import pathlib
files_candidates = list(pathlib.Path(your_dir).glob("**/20.10.2021.txt"))
然后,使用shutil.copy
方法將文件復制到其他目錄; 讓我們假設它是第一個候選人:
import shutil
shutil.copy(files_candidates[0], destination)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.