Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.argv arguments with spaces

Tags:

python

I'm trying to input folder names as sys.argv arguments, but am having problem with folder names that have spaces, which become multiple variables.

For example, from the command line below, "Folder Name" becomes two variables.

Program.py D:\Users\Erick\Desktop\Folder Name 

Any solutions?

like image 974
Erick Brownfield Avatar asked Jun 09 '16 06:06

Erick Brownfield


2 Answers

Space is the delimiter for command line arguments. You'll be better off not using spaces in your directory and file names if possible. For entering an argument which has space in it you'll have to enclose it in quotes "folder with space".

Program.py "D:\Users\Erick\Desktop\Folder Name"

like image 145
Abdul Fatir Avatar answered Oct 01 '22 10:10

Abdul Fatir


Assuming input is always going to be a single file/folder path:

path = " ".join(sys.argv[1:])
like image 21
Arshiyan Alam Avatar answered Oct 01 '22 11:10

Arshiyan Alam