2016-03-28 62 views
1

我使用os.system调用(Python 2.7)以一种钝的方式自动执行一些繁琐的shell任务,主要是文件转换。然而,出于一些奇怪的原因,我的正在运行的解释器似乎无法找到我刚创建的文件。python:无法找到最近更改的目录(OSx)中的文件

示例代码:

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)。 (这足以让其他任务完成。)还没有。

嗯。帮帮我?谢谢!

回答

0

您应该在致电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秒钟等待here

+0

哇,果然是那么简单。我现在觉得很愚蠢。 :-) 谢谢! –