2013-08-21 13 views
15

Chcę dołączyć nazwę pliku "main.txt" w temacie, ponieważ przekazuję nazwę pliku z wiersza poleceń. ale coraz błąd w ten sposóbjak używać konkatenacji stałego ciągu i zmiennej w Python

python sample.py main.txt #running python with argument 

msg['Subject'] = "Auto Hella Restart Report "sys.argv[1] #line where i am using that passed argument 
+0

Prawie zawsze dobrze jest dołączyć ślad stosu - służy on do debugowania! – Brionius

Odpowiedz

26

Zgaduję, że masz na myśli to zrobić:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1] 
# To concatenate strings in python, use  ^
4

Spróbuj

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1] 

Operator + jest przesłonięta w Pythonie do łączenia ciągów .

3

Jeśli trzeba dodać dwa ciągi trzeba użyć operator +

stąd

msg['Subject'] = your string + sys.argv[1] 

a także trzeba importować sys na początku

jak

import sys 

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1] 
1
variable=" Hello..." 
print (variable) 
print("This is the Test File "+variable) 

dla typu całkowitego ...

variable=" 10" 
print (variable) 
print("This is the Test File "+str(variable)) 
Powiązane problemy