簡體   English   中英

使用python glob忽略Windows隱藏文件

[英]Ignoring windows hidden files with python glob

我正在使用python腳本移動某些文件。 該腳本應同時在osx和Windows上運行。

我正在使用glob模塊來選擇文件。 使用os.path中的isfile方法過濾目錄。 glob模塊自動忽略unix。 文件,但似乎確實抓取了一些Windows隱藏文件。 我添加了代碼,以刪除似乎出現在Windows中的一個“ desktop.ini”。

是否還有其他Windows文件可能出現,或者有一種方法可以確保我不在Windows中選擇隱藏文件?

files = glob.glob('*')
files = filter(os.path.isfile, files)  # filter out dirs
if "desktop.ini" in files : files.remove('desktop.ini')
# then using "shutil.move" to actually move the files

您可能需要嘗試Formic

from formic import FileSet
fileset = FileSet(directory="/some/where/interesting",
              include="*.py",
              exclude=["desktop.ini", ".*", "addition", "globs", "here"]
              )
for filename in fileset:
    # use shutil to move them

這是一個使用Globs的Python庫,但是i)已經了解大多數隱藏文件( 此處為內置列表),並且ii)允許您指定要從結果中排除的任何文件( 文檔

披露:我是維護者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM