python:在最近更改的目录(OSx)中找不到文件
我正在通过os.system调用(Python2.7)以一种直截了当的方式自动化一些繁琐的shell任务,主要是文件转换。然而,出于某种奇怪的原因,我正在运行的解释器似乎无法找到我刚刚创建的文件 示例代码:python:在最近更改的目录(OSx)中找不到文件,python,macos,blocking,Python,Macos,Blocking,我正在通过os.system调用(Python2.7)以一种直截了当的方式自动化一些繁琐的shell任务,主要是文件转换。然而,出于某种奇怪的原因,我正在运行的解释器似乎无法找到我刚刚创建的文件 示例代码: import os, time, glob # call a node script to template a word document os.system('node wordcv.js') # print the resulting document to pdf os.syst
import os, time, glob
# call a node script to template a word document
os.system('node wordcv.js')
# print the resulting document to pdf
os.system('launch -p gowdercv.docx')
# move to the directory that pdfwriter prints to
os.chdir('/users/shared/PDFwriter/pauliglot')
print glob.glob('*.pdf')
我希望有一个长度为1的列表和结果文件名,而不是一个空列表
同样的情况也发生在
pdfs = [file for file in os.listdir('/users/shared/PDFwriter/pauliglot') if file.endswith(".pdf")]
print pdfs
我已经亲自检查过了,预期的文件实际上是它们应该在的地方
另外,我觉得os.system被阻塞了,但为了以防万一,在查找文件之前,我还在其中插入了time.sleep(1)
。(这对于其他任务来说已经足够了)仍然没有
嗯,帮帮忙?谢谢 您应该在调用
launch
后添加等待。Launch将在后台生成任务,并在文档打印完成之前返回。您可以输入一些任意的sleep
语句,或者如果您想要,也可以检查文件是否存在,如果您知道预期的文件名是什么
import time
# print the resulting document to pdf
os.system('launch -p gowdercv.docx')
# give word about 30 seconds to finish printing the document
time.sleep(30)
备选方案:
import time
# print the resulting document to pdf
os.system('launch -p gowdercv.docx')
# wait for a maximum of 90 seconds
for x in xrange(0, 90):
time.sleep(1)
if os.path.exists('/path/to/expected/filename'):
break
可能需要超过1秒等待的参考您应该在调用
启动
后添加等待。Launch将在后台生成任务,并在文档打印完成之前返回。您可以输入一些任意的sleep
语句,或者如果您想要,也可以检查文件是否存在,如果您知道预期的文件名是什么
import time
# print the resulting document to pdf
os.system('launch -p gowdercv.docx')
# give word about 30 seconds to finish printing the document
time.sleep(30)
备选方案:
import time
# print the resulting document to pdf
os.system('launch -p gowdercv.docx')
# wait for a maximum of 90 seconds
for x in xrange(0, 90):
time.sleep(1)
if os.path.exists('/path/to/expected/filename'):
break
参考可能需要超过1秒的等待时间哇,真的很简单。我现在觉得自己很笨。:-)谢谢哇,就这么简单。我现在觉得自己很笨。:-)谢谢