Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在脚本中更改目录后,如何返回脚本目录?_Python_Python 3.x - Fatal编程技术网

Python 在脚本中更改目录后,如何返回脚本目录?

Python 在脚本中更改目录后,如何返回脚本目录?,python,python-3.x,Python,Python 3.x,我正在更改函数中的目录,但无法返回脚本所在的目录 我尝试使用\uuu FILE\uuu和sys.argv[0]获取相应的文件名和目录,但没有帮助 #!/usr/bin/env python3 import time import sys, os script = sys.argv[0] script_path = os.path.dirname(script) def fun1(): abspath = os.path.abspath(__file__) print('ab

我正在更改函数中的目录,但无法返回脚本所在的目录

我尝试使用
\uuu FILE\uuu
sys.argv[0]
获取相应的文件名和目录,但没有帮助

#!/usr/bin/env python3
import time
import sys, os

script = sys.argv[0]
script_path = os.path.dirname(script)

def fun1():

    abspath = os.path.abspath(__file__)
    print('abspath = {}'.format(abspath))
    print('file = {}'.format(sys.argv[0]))
    print('__file__ = {}'.format(__file__))
    dname = os.path.dirname(abspath)
    print('dname = {}'.format(dname))
    os.chdir(dname)
    print('CWD - fun1() = {}'.format(os.getcwd()))

def fun2():
    cwd = os.getcwd()
    if cwd != '/xyz/testdir':
        os.chdir("/xyz/testdir")
    print('CWD - fun2() = {}'.format(os.getcwd()))

def main():

    while True:
        print('Entering fun1()')
        fun1()
        print('Entering fun2()')
        fun2()
        print('Sleeping for 5 secs')
        time.sleep(5)
        print()

if __name__ == "__main__":
    main()
实际结果-

Entering fun1()
abspath = /abc/chdir.py
file = chdir.py
__file__ = chdir.py
dname = /abc
CWD - fun1() = /abc
Entering fun2()
CWD - fun2() = /xyz/testdir
Sleeping for 5 secs

Entering fun1()
abspath = /xyz/testdir/chdir.py
file = chdir.py
__file__ = chdir.py
dname = /xyz/testdir
CWD - fun1() = /xyz/testdir
Entering fun2()
CWD - fun2() = /xyz/testdir
Sleeping for 5 secs
预期结果-

Entering fun1()
abspath = /abc/chdir.py
file = chdir.py
__file__ = chdir.py
dname = /abc
CWD - fun1() = /abc
Entering fun2()
CWD - fun2() = /xyz/testdir
Sleeping for 5 secs

Entering fun1()
abspath = /abc/chdir.py
file = chdir.py
__file__ = chdir.py
dname = /abc
CWD - fun1() = /abc
Entering fun2()
CWD - fun2() = /xyz/testdir
Sleeping for 5 secs
i、 e.在第二次(和后续迭代)期间,脚本应返回到原始目录。
提前感谢。

根据我的理解,您希望在第二次迭代中使用与第一次迭代相同的
功能1
工作目录。因此
函数1
将使用脚本目录返回始终相同的工作目录

import time
import sys, os
script = sys.argv[0]
ab_path = os.path.abspath(__file__)
s_directory=os.path.dirname(ab_path)

def fun1():
    abspath = os.path.abspath(__file__)
    print ('script path =================={}',s_directory)
    print('abspath = {}'.format(abspath))
    print('file = {}'.format(sys.argv[0]))
    print('__file__ = {}'.format(__file__))
    print('dname = {}'.format(s_directory))
    os.chdir(s_directory)
    print('CWD - fun1() = {}'.format(os.getcwd()))
    print('**************************************fun1*******************************')

def fun2():
    cwd = os.getcwd()
    if cwd != '/home/test/testdir':
     os.chdir("/home/test/testdir")
     print('CWD - fun2() = {}'.format(os.getcwd()))
     print('**************************************fun2*******************************')

def main():
    while True:
     print('Entering fun1()')
     fun1()
     print('Entering fun2()')
     fun2()
     print('Sleeping for 5 secs')
     time.sleep(5)
     print()

if __name__ == "__main__":
    main()

根据我的理解,在第二次迭代中,您希望在
函数1
中使用与第一次迭代中相同的工作目录。因此
函数1
将使用脚本目录返回始终相同的工作目录

import time
import sys, os
script = sys.argv[0]
ab_path = os.path.abspath(__file__)
s_directory=os.path.dirname(ab_path)

def fun1():
    abspath = os.path.abspath(__file__)
    print ('script path =================={}',s_directory)
    print('abspath = {}'.format(abspath))
    print('file = {}'.format(sys.argv[0]))
    print('__file__ = {}'.format(__file__))
    print('dname = {}'.format(s_directory))
    os.chdir(s_directory)
    print('CWD - fun1() = {}'.format(os.getcwd()))
    print('**************************************fun1*******************************')

def fun2():
    cwd = os.getcwd()
    if cwd != '/home/test/testdir':
     os.chdir("/home/test/testdir")
     print('CWD - fun2() = {}'.format(os.getcwd()))
     print('**************************************fun2*******************************')

def main():
    while True:
     print('Entering fun1()')
     fun1()
     print('Entering fun2()')
     fun2()
     print('Sleeping for 5 secs')
     time.sleep(5)
     print()

if __name__ == "__main__":
    main()

\uuuuu file\uuuu
是脚本文件的名称,不带启动目录。当您使用
abspath
时,您可以通过当前目录获得它。
如果更改当前目录,
abspath
将返回其他内容。因此,如果您想捕获启动目录,只需使用func2中使用的
os.path.getcwd()

\uuuuu file\uuuu
是脚本文件的名称,而不包含启动目录。当您使用
abspath
时,您可以通过当前目录获得它。
如果更改当前目录,
abspath
将返回其他内容。因此,如果要捕获启动目录,只需像在func2中使用的那样使用
os.path.getcwd()

即使在
fun2()
中更改目录后使用
os.path.getcwd()
,它也会打印更改的目录名。即使在
fun2()
中更改目录后使用
os.path.getcwd()
,它打印更改的目录名。