简体   繁体   中英

Adding Variable into a file path variable

I am taking below input:

id = input("Enter the minion name you want to connect : Ex: HK_5871_f - ")

Now I want the input provided by user as HK_5871_f :

path="/var/cache/salt/master/minions/$id"
print (path)

The output should be as below:

/var/cache/salt/master/minions/HK_5871_f

But I am getting the below output:

/var/cache/salt/master/minions/$id

Thanks in Advance.

If you are using python 3.6+ you can use f-string formatting

An Example:

id =  "HK_5871_f"
path = f'/var/cache/salt/master/minions/{id}'
print(path)

Output:

/var/cache/salt/master/minions/HK_5871_f

The issue is due to the assignment of id as text instead of its value HK_5871_f .

You should set path variable somewhat like below:

path = "/var/cache/salt/master/minions/%s" % id
print(path)

This can easily be done with the "+" operator since it is a string. This operation is called concatenation.

The code below should work fine:

id = input("Enter the minion name you want to connect : Ex: HK_5871_f - ")
path = "/var/cache/salt/master/minions/"+id

Output:

/var/cache/salt/master/minions/HK_5871_f

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