Python string format - python

I have defined the string variable
value=“c:/program files/tm su/usr”
I need to use this variable in another string like
Bashcmd=“Override project={value}”
I tried adding the rf option like this
Bashcmd =rf“Override {value}”
But it’s printing only until c:/program, white spaces are neglected.
Is there any way to use entire path in Bashcmd and can’t remove spaces in directory path because many system share same paths.

You can format strings like this:
value="c:/program files/tm su/usr"
Bashcmd=f"Override project=\"{value}\""
or you can simply concatenate the string like this:
Bashcmd="Override project=\""+value+"\""

Related

Is it possible to use format() function with a string in format's place?

I'm using a format() in python and I want to use a variable pokablelio so that the person could choose how many numbers to output after the dot. When I try to put the variable alone after the comma it outputs: ValueError: Invalid format specifier. I tried replacing some characters or making the whole string in a parentheses but that didn't work.
Right now I'm wondering: Can I even use a variable as a string to put it in format's place?
(note: The machine should have a "'.10f'" string in the variable)
Error and the code
It is possible to use variables as part of the format specifier, just include them inside additional curly braces:
>>> n_places = 10
>>> f'{1.23:.{n_places}f}'
'1.2300000000'

How to Read A String Literal Stored in a Variable

I was wondering if there was a way to read a string literal stored in a variable. I was essentially trying to extract the file name for a variable containing a file path. I'm aware that you need to place r' before the path name. In my example below, the variable I'm trying to update is 'test'. So basically I'm unaware of how I can use r' on the variable name to avoid parts of the path being read as unicode characters. Is there a way to do this?
test='NAI\site_summaries\410_-_407_Central'
head,tail=os.path.split(test)
print(tail)
The code above returns 'site_summaries_-_407_Central', where it should be returning '410_-_407_Central'. Please keep in mind that I have a variable containing a list of these paths but I just chose to show one path for the sake of simplicity.

Getting (and appending) correct file path in Python 2.7

I have a previously defined file name in a string format, and a previously defined variable called value. I am trying to store a variable that looks like:
C:\Users\Me\Desktop\Value_Validation_Report
with the syntax below, I instead get:
C:\Users\Me\Desktop\Value\ _Validation_Report
target_dir= os.path.dirname(os.path.realpath(FileName))
ValidationReport=os.path.join(target_dir,value,"_Validation_Report")
print ValidationReport
Every other combination I have tried leads to an error. Any help would be greatly appreciated!
If value is a String, you must concatenate that with "_Validation_Report
target_dir= os.path.dirname(os.path.realpath(FileName))
ValidationReport=os.path.join(target_dir,value + "_Validation_Report")
print ValidationReport
os.path.join will add a separator (which depends on the operating system) between each string you give it. To avoid this, simply put your value and "_Validation_Report" strings together as one String. See more about os.path.join.

Python subprocess module: How can I pass comma separated input into Popen?

I have a list of strings and a command I'd like to run with Popen. The command takes the strings as input arguments.
How can I easily add the entire list...
list=['asdf','qwer','zxcv',...]
...as comma separated input shown below:
Popen(['cmd','asdf','qwer','zxcv',...])
I won't be able to do this because it won't convert list to str implicitly:
Popen(['cmd',list])
Nor this, because it simply won't allow for spaces within a string:
Popen(['cmd',' '.join(list)])
Is there an alternative?
I do not want to use the 'shell=True' option.
You can do the following to create a new list from two (or more) separate lists.
['cmd'] + list
This creates a new list for you with the contents of both. As you mentioned, the syntax looks and does exactly as you expect, which is adding two lists together.
Note: I would also like to warn that you shouldn't use list as a variable name. Since this means you are shadowing the built-in list type. Which could cause unforeseen problems later.

Python -- Quotations around filenames

I'm not quite sure when I need to put quotations around the filenames in Python.
For example, when I set
f = open(file)
I can run something like
len(f.read())
and it will run fine.
However, when I do it directly, it only works with
len(open("file").read())
Likewise, in terminal when running from Python I always have to use quotations.
What is the 'rule' when using quotations?
Thank you.
In python you can always use the name of a variable or function outside quotations, but the name of a file is usually not a variable.
If file is the name of a string variable you can always do open(file).read(), however if it is literally the filename you must always do open("file").read().
Quotations indicate a string literal constant. No quotations indicate that you're referencing a variable, which may itself be a string (in this case, populated with the path to a file).

Categories

Resources