Reputation: 2304
I need to identify where some powershell path strings cross over into Python.
How do I detect if a path in Python starts with .\
??
Here's an example:
import re
file_path = ".\reports\dsReports"
if re.match(r'.\\', file_path):
print "Pass"
else:
print "Fail"
This Fails, in the debugger it lists
expression = .\\\\\\
string = .\\reports\\\\dsReports
If I try using replace like so:
import re
file_path = ".\reports\dsReports"
testThis = file_path.replace(r'\', '&jkl$ff88')
if re.match(r'.&jkl$ff88', file_path):
print "Pass"
else:
print "Fail"
the testThis
variable ends up like this:
testThis = '.\\reports&jkl$ff88dsReports'
Quite agravating.
Upvotes: 4
Views: 234
Reputation: 6748
The reason this is happening is because \r
is an escape sequence. You will need to either escape the backslashes by doubling them, or use a raw string literal like this:
file_path = r".\reports\dsReports"
And then check if it starts with ".\\"
:
if file_path.startswith('.\\'):
do_whatever()
Upvotes: 6