簡體   English   中英

從具有相同名稱但文件類型不同的其他文件中查找沒有擴展名的文件

[英]Finding one file without extension from other files with same name but different file type

我有一個包含許多不同文件的文件夾,都稱為報告。 一些沒有文件擴展名。

在此輸入圖像描述

我需要使用python或bash將pdf報告移動到另一個文件夾。

關於如何做到這一點的任何提示?

如果你所做的只是移動到另一個文件夾,我會推薦bash,因為它是一個如此簡單的操作

#!/bin/bash
mv report.pdf /path/to/new/folder

我同意AllenMoh對bash的回答。 由於OP也詢問了Python,這里是Python解決方案。

import shutil
import os

src = '/path/report.pdf'
dst = '/new_path/'

shutil.move(src, dst)

# alternative, if destination is on current file system, you can use rename
# notice here the filename is specified
dst = '/new_path/report.pdf' 

os.rename(src, dst)

您可以使用Python-Magic庫幫助來查找文件類型。

讀取循環中的所有magic.from_file("filename")並將其傳遞給magic.from_file("filename")並檢查輸出以查看它是否為pdf。 如果是pdf,則將其移至目標目錄。

此api不依賴於文件擴展名。

暫無
暫無

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

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