Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将PDF报告排序到适当的目录中”;TypeError:应为str、bytes或os.PathLike对象,而不是list";_Python_Sorting_Rename - Fatal编程技术网

Python 将PDF报告排序到适当的目录中”;TypeError:应为str、bytes或os.PathLike对象,而不是list";

Python 将PDF报告排序到适当的目录中”;TypeError:应为str、bytes或os.PathLike对象,而不是list";,python,sorting,rename,Python,Sorting,Rename,我当前收到一个错误: line 24, in <module> pdfFileobj = open(pdfFiles, 'rb') TypeError: expected str, bytes or os.PathLike object, not list" 首先,glob.glob(str)返回一个列表,因此不需要添加额外的方括号 其次,open() 更改此部分: while pdfFiles: pdfFileobj = open(pdfFile

我当前收到一个错误:

line 24, in <module> pdfFileobj = open(pdfFiles, 'rb') TypeError: expected str, bytes or os.PathLike object, not list"
  • 首先,
    glob.glob(str)
    返回一个列表,因此不需要添加额外的方括号

  • 其次,
    open()

更改此部分:

while pdfFiles:
        pdfFileobj = open(pdfFiles, 'rb')
        pdfReader = p2.PdfFileReader(pdfFileobj)
        pdfFiles.pop(-1)
for file in pdfFiles:
        pdfFileobj = open(file, 'rb')
        pdfReader = p2.PdfFileReader(pdfFileobj)
至:

while pdfFiles:
        pdfFileobj = open(pdfFiles, 'rb')
        pdfReader = p2.PdfFileReader(pdfFileobj)
        pdfFiles.pop(-1)
for file in pdfFiles:
        pdfFileobj = open(file, 'rb')
        pdfReader = p2.PdfFileReader(pdfFileobj)
(还要注意,
pop()
默认情况下会删除
-1
索引,因此不必传入参数)


总而言之:

import os
import winreg
import PyPDF2 as p2
import glob

def get_download_path():
    """Returns the default downloads path for linux or windows"""
    if os.name == 'nt':
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')

os.chdir(get_download_path())

pdfFiles = glob.glob("*.pdf")
pdfs = []

for file in pdfFiles:
        pdfFileobj = open(file, 'rb')
        pdfReader = p2.PdfFileReader(pdfFileobj)

____________________________更新循环:____________________________

import os
import winreg
import PyPDF2 as p2
import glob
from time import sleep

def get_download_path():
    """Returns the default downloads path for linux or windows"""
    if os.name == 'nt':
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')

os.chdir(get_download_path())

while True:
    pdfFiles = glob.glob("*.pdf")
    pdfs = []

    for file in pdfFiles:
            pdfFileobj = open(file, 'rb')
            pdfReader = p2.PdfFileReader(pdfFileobj)
    sleep(300) # stop the program for 300 seconds

如果我想多次迭代此代码,因为在此场景中我无法使用while循环,该怎么办?为什么要多次迭代此代码?我想多次迭代,因为Pdfiles列表每天更新20-50次。嗯,您可以使用
while
循环。。。你想让我把它加到帖子上吗?哦,真的吗?那太好了!