简体   繁体   中英

Python: New line character after command

In a program I am writing I have this:

SUDOPATH = subprocess.Popen(['which', 'sudo'], stdout=subprocess.PIPE)
SUDO = SUDOPATH.stdout.read()

But if I attempt to do this:

os.makedirs(SUDO + directory)

It gives me an error:

OSError: [Errno 13] Permission denied: '/usr/bin/sudo\n'

How do I go about removing the new line character so I don't run in to this problem when issuing shell commands from Python?

Try:

SUDO[:-1] 
#prints '/usr/bin/sudo'

That worked for me

EDIT: or

SUDO=SUDO.strip()

os.makedirs(SUDO.strip() + directory)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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