![](/img/trans.png)
[英]“no such file or directory” in python script calling other python script in debian linux
[英]Python - Called Script is searching for a file in the Calling Script's directory
我有以下結構
C:\Users\dhiwakarr\workspace\BasicRegressionOnJoker\create&&bkp\script1.py
script1.py
將調用位於script2.py
的函數/方法,位於
C:\Users\dhiwakarr\workspace\basics\script2.py
問題是script2.py
將使用XML File (create.xml)
,該文件與script2.py
位於同一文件夾中。 但是,當我在script2.py
FROM script1.py
調用此方法時。 我收到以下錯誤,
execute: Error 0x304: Failed to read the input file[createsc.xml].
Traceback (most recent call last):
File "create&&bkp.py", line 19, in <module>
CreateSC.create()
我的猜測是,被調用腳本( script2.py
)正在調用腳本( script1.py
)中搜索此文件。 如何使script1.py中調用的script2.py方法使其在自己的目錄中搜索?
更新
script1.sc
import subprocess,sys,getopt,codecs,re,string
import xml.etree.ElementTree as ET
sys.path.insert(0,r'C:\Users\dhiwakarr\workspace\basics')
import Login
import script2
#import script3
try:
#First call the login script to login
print('Login started')
Login.login()
print('Create Subclient')
script2.create()
....
script2.py
import subprocess,sys,os,inspect
from sys import stdout
from _winapi import NULL
def create():
'''
A text file with information about the Client,Storage Policy,Backupset,Subclient & Content of each subclient must be given as seen in sample-create.txt
'''
inputfile = r'C:\Users\dhiwakarr\workspace\create.txt'
finp = open(inputfile,'r')
path = str(os.getcwd())
print('Current Working Path is -- '+path)
for line in finp:
line=line.rstrip('\n')
....
# Creating the Subclient
subprocess.check_call(["C:\\Program Files\\CommVault\\Simpana\\Base\\qoperation.exe", 'execute', '-af', `'createsc.xml',` '-appName', "'File System'",'-clientName', client,'-backupsetName', bset, '-subclientName', scname, '-storagePolicyName', storagepolicy])
else:
參見subprocess.check_call([“ ...行,它無法讀取XML。
給出的信息很少,更多的代碼會有所幫助。
通常,從命令行調用腳本時,基本路徑將是路徑,shell定向到的不是調用路徑。
要獲取有關腳本在哪里尋找文件的信息,請插入
print os.getcwd()
在適當的位置(在打開命令或文件命令之前)。 但是,您需要從電池組中導入“ os”。
此外,為了更好地理解潛在問題,請使用
try:
f = open(f)
…
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except:
print "Unexpected error:", sys.exc_info()[0]
raise
可能會更好地理解潛在問題。
您添加的代碼信息:
更改sys.Path(.extend; .insert)不會更改文件I / O的目錄。 sys.Path僅定向模塊加載器。 使用os.chdir或相對路徑進行文件I / O。 使用上面的print os.getcwd()方法可獲得有關代碼在何處查找.xml文件的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.