我試圖在Python腳本中執行批處理命令,只是顯示PDF文件的名稱。基本上,Python腳本位於文件夾C:\users\me\desktop\python
中,該文件夾應該在桌面上的其他文件夾(C:\users\me\desktop\some-folder
)上執行命令,該文件夾中包含PDF子文件夾。在Python腳本中運行批處理命令
下面是代碼:
from subprocess import call
import os
for root, dirs, files in os.walk("../some-folder"):
for pdf_file in files:
if pdf_file.endswith(".pdf"):
pdf_file_path = os.path.join(root, pdf_file)
os.chdir(root)
call('for %%f in (*.pdf) do @echo %%f')
結果我得到的是 「找不到文件」。
如果您已經使用python獲得pdf名稱,爲什麼要使用cmd.exe FOR命令重試它們?我不明白這是一個批處理文件問題。在這個問題中你確實沒有批處理文件代碼。 – Squashman