How to handle spaces with spaces in Linux when coding in Python 2.7?

I have a python script that processes files in a directory in Linux Mint. Part of the code is as follows:

path_to_dir = "/home/user/Im a folder with libs to install/" if os.path.isdir(path_to_dir): print "it can locate the directory" os.chdir(path_to_dir) # everything ok here :D subprocess.call(['./configure'], shell = True) subprocess.call(['make'], shell = True) subprocess.call(['make install'], shell = True) # problem happens here 

When executing subprocess.call(['make install'], shell = True) it throws this error:

 /bin/bash: /home/user/Im: No such file or directory make[3]: *** [install-libLTLIBRARIES] Error 127 make[2]: *** [install-am] Error 2 make[1]: *** [install-recursive] Error 1 make: *** [install-recursive] Error 1 

How can I use spaces in paths when doing subprocess.call(['make install'], shell = True) ? (I am using Python 2.7)

Edit: I found out the source of the errors: The Makefile from the libraries I use (downloaded from somewhere on the Internet) does not support spaces in the path.

I tested the answer given here using this code in a terminal located on the path "/ home / user / Im a folder with libs to install /" and using the root account:

 ./configure make make install /home/user/Im\ a\ folder\ with\ libs\ to\ install/Makefile 

This gives me the same error:

 /bin/bash: /home/user/Im: No such file or directory [install-libLTLIBRARIES] Error 127 make[3]: se sale del directorio «/home/user/Im a folder with libs to install/» make[2]: *** [install-am] Error 2 make[2]: se sale del directorio «/home/user/Im a folder with libs to install/» make[1]: *** [install-recursive] Error 1 make[1]: se sale del directorio «/home/user/Im a folder with libs to install/» make: *** [install-recursive] Error 1 

It can find the folder, but it seems that inside it passes the Linux bash path without scape characters.

I accept nafas as an answer because it helped me find the source of the problem.

0
Jan 20 '16 at 16:30
source share
2 answers

skip spaces using \

eg.

Im\ a\ folder\ with\ libs\ to\ install

0
Jan 20 '16 at 16:33
source share

You should be able to replace spaces with your shielded versions. Try:

 path_to_dir = path_to_dir.replace(" ", "\\ ") 
0
Jan 20 '16 at 16:35
source share



All Articles