2014-02-23 40 views
0

這是代碼,我遇到的問題是,儘管一切按預期工作,除非指定完整的文件路徑(如在「s」中),否則不能調用所需的perl腳本。 ,即使它們都在同一個目錄(桌面)中。任何幫助,將不勝感激。從python調用perl沒有完整的文件路徑

import subprocess 
status = ['s', 'mj', 'ms', 'h','q'] 
while(1): 
    while(1): 
     print("Compute income tax: \n") 
     print("\ts = single \n") 
     print("\tmj = married and filing jointly \n") 
     print("\tms = married and filing seperately \n") 
     print("\th = head of household \n") 
     print("\tq = quit \n") 
     #get user input for status 
     choice=raw_input("Enter status: ") 
     if (choice in status): 
      print("58") 
      break 
     else: 
      print("Unknown status!") 
    if (choice=='q'): 
     break 
    else: 
     if (choice=="s"): 
      subprocess.call('perl C:/Users/Username/Desktop/single.pl') 
     elif(choice=='mj'): 
      subprocess.call('perl marriedJointly.pl') 
     elif(choice=='ms'): 
      subprocess.call('perl marriedSeperately.pl') 
     elif(choice=='h'): 
      subprocess.call('perl head.pl') 
      #and so on 

回答

0

您是否正在調用像C:\>python myscript.py這樣的腳本?如果是這樣,那麼你的腳本實際上將在python程序下運行,這意味着它的位置將在python可執行文件的位置,而不是腳本。您可以通過導入os並運行os.getcwd()來驗證此問題,這可能會顯示類似'C:\\Python33'的內容。

爲了解決這個問題,無論是直接通過做C:\myscript.py調用腳本(該工程只是finel;的Windows知道使用基於斷擴展其解釋器),使用os.chdir()以修復文件系統中的位置,或者只是使用絕對尋路。

+0

呃,不,我打開命令提示符窗口並輸入完整的文件路徑並執行它。我打開cmd並將文件拖到窗口中,然後按回車。 – user2167980

+0

哦,明白了,這是其他文件的問題,謝謝你的幫助。 – user2167980