2012-05-29 3 views
3

파이썬 스크립트를 실행할 때 공백으로 인수를 읽으려면 어떻게해야합니까? 내가 실행하지 않을 경우,쉘 스크립트에서 파이썬 스크립트의 공백으로 인수 읽기

> python script.py firstParam file\ with\ spaces.txt 
# or 
> python script.py firstParam "file with spaces.txt" 

# script.py 
import sys 
print sys.argv 

그러나 :

이 작동 :

UPDATE : 내 문제는 내가 쉘 스크립트를 통해 파이썬 스크립트를 호출하는거야 있다는 것입니다 같은

이 보이는 그것은 스크립트를 통해 :

myscript.sh :

#!/bin/sh 
python [email protected] 

인쇄 : [ 'firstParam', '파일', 'spaces.txt' '와']

하지만 내가 원하는이다 : 공백 [ 'firstParam', '파일. 대신 TXT ']

답변

4

다른 프로그램에 쉘 스크립트에서 매개 변수를 전달하려면

, 당신은 "[email protected]" 대신 [email protected] 사용해야로 /tmp/test.py
$ /tmp/test.sh /tmp/test.py firstParam "file with spaces.txt" 
['/tmp/test.py', 'firstParam', 'file with spaces.txt'] 

정의. 이렇게하면 공백이 포함되어 있어도 각 매개 변수가 단일 단어로 확장됩니다. [email protected]$1 $2 ...과 동일하고, "[email protected]""$1" "$2" ...과 같습니다. 예를 들어

, 당신은 실행하는 경우 : ./myscript param1 "param with spaces" :

  • [email protected]param1 param with spaces로 확장됩니다 - 네 개의 매개 변수.
  • "[email protected]"은 두 개의 매개 변수 "param1" "param with spaces"으로 확장됩니다.
관련 문제