执行ranger-admin setup.sh报错TypeError: a bytes-like object is required, not 'str'

报错代码:

报错内容:

Traceback (most recent call last):
  File "db_setup.py", line 1455, in <module>
    main(sys.argv)
  File "db_setup.py", line 1422, in main
    run_env_file(env_file_path)
  File "db_setup.py", line 167, in run_env_file
    set_env_val(command)
  File "db_setup.py", line 156, in set_env_val
    (key, _, value) = line.partition("=")
TypeError: a bytes-like object is required, not 'str'
def set_env_val(command):
        proc = subprocess.Popen(command, stdout = subprocess.PIPE)
        for line in proc.stdout:
                (key, _, value) = line.partition("=")
                os.environ[key] = value.rstrip()
        proc.communicate()

只需要在将line转化为string即可,需要注意的是python中空格和tab不能混用,否则报错inconsistent use of tabs and spaces in indentation
如下:

def set_env_val(command):
	proc = subprocess.Popen(command, stdout = subprocess.PIPE)
	for line in proc.stdout:
			line_str = line.decode("utf-8")
			(key, _, value) = line_str.partition("=")
			os.environ[key] = value.rstrip()
	proc.communicate()

posted @ 2026-01-21 14:38  lisacumt  阅读(2)  评论(0)    收藏  举报