Python调用的脚本正在调用脚本的目录中搜索文件

2024-06-26 14:51:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下结构

C:\Users\dhiwakarr\workspace\BasicRegressionOnJoker\create&&bkp\script1.py

script1.py将调用在script2.py中定义的函数/方法,该函数/方法位于

^{pr2}$

问题是script2.py将使用与script2.py位于同一文件夹中的{}。但是当我从script1.py调用script2.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方法在自己的目录中进行搜索?在

更新

脚本1.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_调用(["... 它无法读取XML。在


Tags: the方法pyimportcreatesyslinexml
1条回答
网友
1楼 · 发布于 2024-06-26 14:51:11

给出的信息是稀疏的,更多的代码将是有帮助的。在

一般来说,当从命令行调用脚本时,基本路径将是路径,shell被定向到不是调用的路径。在

若要获取有关脚本在何处查找文件的信息,请插入

print os.getcwd() 

在适当的位置(在“打开”或“文件”命令之前)。但你需要从电池组中导入“操作系统”。在

此外,为了更好地理解潜在的问题,使用

^{pr2}$

可能会让你更好地理解潜在的问题。在

your added code information:

变更系统路径(.extend;.insert)不会更改文件I/O的目录。系统路径只指示模块加载程序。使用os.chdir公司或文件I/O的相对路径。使用上面的打印操作系统getcwd()方法获取有关代码在何处查找.xml文件的详细信息。在

相关问题 更多 >