How to concatenate strings to build full path? [duplicate] - python

This question already has answers here:
Function to concatenate paths?
(2 answers)
String formatting in Python [duplicate]
(14 answers)
How to concatenate a fixed string and a variable in Python
(6 answers)
Closed 2 years ago.
path = r'C:\Program Files\1\1.exe'
I can run a program using the path above. I don't want to hardcode it, so I'm trying to use a variable in place of the 1s. This is what I came up with:
variable1 = '1'
path = r'C:\Program Files\ , +variable1 , +\, +variable1, +.exe'
This doesn't work. How do I make it work?

variable1 = '1'
path = 'C:\\Program Files\\'+variable1+'\\'+variable1+'.exe'
\ is special char so you need to escape it with another \

Related

Adding a absolute path that has a \f in it [duplicate]

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 3 months ago.
While adding an absolute path to my script because it has a \f in it the code won't run properly.
C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx
The file is in the same directory as the script but using a relative path won't work. No misspellings or anything.
Use python r string
path=r'C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx'
Use one of the following ways:
r"C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx"
"C:\\Users\\showoi\\Desktop\\website\\repository\\fileAdder\\softwarelisting.xlsx"
"C:/Users/showoi/Desktop/website/repository/fileAdder/softwarelisting.xlsx

Changing `\` to `/` [duplicate]

This question already has answers here:
Python replace / with \
(3 answers)
Closed 8 months ago.
I would like to go the opposite direction of the post Python replace / with \.
For example, the path to desktop:
import os
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
print(desktop)
The above code prints "C:\Users\r2d2w\Desktop". I would like to convert it to "C:/Users/r2d2w/Desktop". How can this be done? The line desktop.replace("\", "/") will not work and will give error "EOL while scanning string literal".
Have you already tried to do:
desktop.replace("\\", "/")
?

How to extract the filename from a string using regular expression [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Extract file name with a regular expression
(3 answers)
Get Filename Without Extension in Python
(6 answers)
Extracting extension from filename in Python
(33 answers)
Regex: Get Filename Without Extension in One Shot?
(10 answers)
Closed 3 years ago.
I am new in the regular expression and trying to extract the file name from a string which is basically a file path.
string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes
i know i can do this by below code.
string.split('/')[2].split('.')[0]
but I am looking to do this by using regular expression so if in future the formate of the path changes(input_new/survey/path/path/argentina-attributes.csv) should not affect the output.
i know kind of similar question asked before but I am looking for a pattern which will work for my use case.
Try this,
>>> import re
>>> string = "input_new/survey/argentina-attributes.csv"
Output:
>>> re.findall(r'[^\/]+(?=\.)',string) # or re.findall(r'([^\/]+)\.',string)
['argentina-attributes']
Referred from here
Try this:
string = "input_new/survey/argentina-attributes.csv"
new_string = string.split('/')[-1].split('.')[0]
print(new_string)

How can I split string to several folders? [duplicate]

This question already has answers here:
How to split a dos path into its components in Python
(23 answers)
Closed 4 years ago.
I have a case like this:
Path = 'C:\Intel\ExtremeGraphics\CUI\Resource'
I want to split this string to list with the string folders names:
['C:', 'Intel', 'ExtremeGraphics', 'CUI, 'Resource']
How can I do that in the shortest way?
folders = Path.split('\')
It will return you the required list of the folders

Python adds x0 to integers [duplicate]

This question already has an answer here:
Python 3.4.1 script syntax error, arcpy & [duplicate]
(1 answer)
Closed 7 years ago.
I am very new to Python and am getting this small problem. I am using Python 3.3
There is a variable I declared in my code
file_name = "resource\email\ham\6.txt"
However, when I look for the variable, it appends additional numbers
>>file_name
'resource\\email\\ham\x06.txt'
Is there a reason why it behaves as so? If not, how do I remove those additional characters? Also, why are they there?
Use r raw string:
file_name = r"resource\email\ham\6.txt"
Double \:
file_name = "resource\\email\\ham\\6.txt"
Or /:
file_name = "resource/email/ham/6.txt"
\ has a special meaning in python, it is used to escape characters.

Categories