Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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
IOError:Errno 13在Powershell中运行python循环时被拒绝的权限D:/PDFs_Python_Powershell - Fatal编程技术网

IOError:Errno 13在Powershell中运行python循环时被拒绝的权限D:/PDFs

IOError:Errno 13在Powershell中运行python循环时被拒绝的权限D:/PDFs,python,powershell,Python,Powershell,我正在Windows Powershell中运行一个循环,该循环在文件目录下运行来自pdfminer、pdf2txt.py的脚本。以下是循环: $PATH="D:/PDFdirectory" foreach ($f in $PATH) { python pdf2txt.py -o $f.txt "$f" "${f%.pdf}.txt" } 当我尝试在Powershell中运行上面的代码时,出现“权限被拒绝”错误。错误指向下面pdf2txt脚本中的outpp=file(outfile,'

我正在Windows Powershell中运行一个循环,该循环在文件目录下运行来自pdfminer、pdf2txt.py的脚本。以下是循环:

$PATH="D:/PDFdirectory"

foreach ($f in $PATH)
{   
python pdf2txt.py -o $f.txt "$f" "${f%.pdf}.txt"
}
当我尝试在Powershell中运行上面的代码时,出现“权限被拒绝”错误。错误指向下面pdf2txt脚本中的outpp=file(outfile,'w+b')

if outfile:
    outfp = file(outfile, 'w+b')
else:
    outfp = sys.stdout
if outtype == 'text':
    device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
                           imagewriter=imagewriter)
elif outtype == 'xml':
    device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
                          imagewriter=imagewriter)
elif outtype == 'html':
    device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale,
                           layoutmode=layoutmode, laparams=laparams,
                           imagewriter=imagewriter)
elif outtype == 'tag':
    device = TagExtractor(rsrcmgr, outfp, codec=codec)
else:
    return usage()
for fname in args:
    fp = file(fname, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    for page in PDFPage.get_pages(fp, pagenos,
                                  maxpages=maxpages, password=password,
                                  caching=caching, check_extractable=True):
        page.rotate = (page.rotate+rotation) % 360
        interpreter.process_page(page)
    fp.close()
device.close()
outfp.close()
return

if __name__ == '__main__': sys.exit(main(sys.argv))
我已经将pdf2txt.py中的读写条件更改为二进制文件,以与Windows兼容,但现在我卡住了。有人能帮我吗


谢谢

您的第一个问题是PowerShell脚本中的语法不正确

此位:

"${f%.pdf}.txt"
请求查找名为
f%.pdf的变量,并将“.txt”添加到其值以创建字符串。你没有这样的变量,所以你只能得到“.txt”

您的第二个问题(我猜是这样的)是您似乎想要迭代该目录中的所有PDF文件。但你还没有指示PowerShell这么做

总之,我认为您需要以下代码:

$PATH="D:/PDFdirectory"

foreach ($file in Get-ChildItem $PATH -Include *.pdf) {
    python pdf2txt.py -o "$($f.BaseName).txt" -O $f.DirectoryName ($f.FullName -replace '.pdf$','.txt')
}
更多说明:

  • $f.DirectoryName
    -包含文件的目录的路径
  • $f.BaseName
    -不带扩展名的文件名
  • “$($f.BaseName).txt”
    -括号标记了将在构造最终字符串之前执行的子表达式
  • ($f.FullName-replace.pdf$,'.txt')
    -使用正则表达式replace在完整文件名(和路径)的末尾查找
    .pdf
    ,并将其替换为
    .txt

您是否以管理员身份运行Powershell?是的,我是以管理员身份启动它的。
foreach($PATH中的f)
是否满足您的要求?它看起来只有一个条目—目录名—并且没有枚举该目录中的文件。DirectoryName是否指向文件的去向或pdf2txt.py的去向?和$f.BaseName==file?PDF文件的位置。