2012-07-05 41 views
0

我有這樣的線在Linux上工作正常:使用字符串的文件名

input_file = '%s/my_input.html' % settings.FILE_PATH 
args =['wkhtmltopdf', input_file, '-'] 
popen = Popen(args, stdout=PIPE, stderr=PIPE) 

在Windows wkhtmltopdf不能同時在Linux上我已經完全沒有問題。我認爲這是斜線/反斜槓相關的問題,但不知道如何解決它。已經嘗試使用反斜槓和雙反斜槓。

有什麼建議嗎?在所有這些變型

同樣的錯誤:

"C:\\tmp\\input.html" 

"C:\tmp\input.html" 

os.path.join("C:\\tmp", "input.html") 

'%s%sinput.html' % ('C:\\tmp', os.path.sep) 

WindowsError在/管理/ salidas/PDF/[錯誤2]埃爾SISTEMA沒有puede hallar EL檔案館especificado

這意味着系統找不到指定的文件。

這對POPEN線

+0

什麼是你的錯誤信息? – dan04 2012-07-05 23:32:50

+0

我認爲這個消息的意思是「系統找不到指定的文件」 – Levon 2012-07-06 00:13:16

+0

我會在你指定字符串的地方使用原始字符串,並且你確定文件存在於你試圖打開它的地方嗎?你可以看到它在python shell裏面,當你執行一個'import os'後跟'os.listdir(your_directory_path_here)'。該文件是否顯示在列表中? – Levon 2012-07-06 00:16:58

回答

1

一般來說嘗試路徑打交道時使用原始字符串..即前掛起的r你的路徑名稱之前。例如,

my_path = 'c:\test\bob` 

my_path = r'c:\test\bob` 

更好,因爲它會阻止在被解釋對你關心的路徑規範\t\b

另外,看看所述os.path模塊以其OS特定的功能/信息再哪個路徑分離器(os.sep)給定的OS上使用,並且join()功能等 - 這將是優選使用字符串直接到指定/操作路徑。

+0

更新的問題,在popen行上的同一錯誤 – juanefren 2012-07-06 00:11:36

2

不要將文件路徑視爲簡單的字符串,而是使用python庫類來處理它們作爲路徑。在這種情況下os.path.join

input_file = os.path.join(settings.FILE_PATH, my_input.html) 
+0

更新的問題,上面的popen行同樣的錯誤 – juanefren 2012-07-06 00:13:40

0

使用os.path.sep,它返回OS依賴路徑分隔符(\\在Windows和/在Linux上)

>>> import os 
>>> FILE_PATH='somepath' 
>>> input_file='%s%smy_input.html' % (FILE_PATH,os.path.sep) 
>>> input_file 
'somepath\\my_input.html' #on windows on linux it returns 'somepath/my_input.html' 
+0

更新的問題,同樣的錯誤上面的popen行 – juanefren 2012-07-06 00:12:59

+0

@juanefren檢查了這一點: 2012-07-06 00:27:39

相關問題