The Sorrows of A Young Coder (Taking It Raw)
In Search of Lost Time (Windows Path Format)
I am a hard core Linux user and in love with Python. I used MS ecosystem from the 90s and started with DOS, through Windows 95, until Windows 10. Therefore, the directory path format that uses backslashes instead of forward-slashes was nothing new to me. But since I started my new coding career on Linux, I am often unaware of the problems people could have when coding on Windows.
Pride and Prejudice (An Easy Coding Task?)
The other day I was going through the coding gigs on UpWork and there was a simple task for which a client was paying $75.
I need a single Python 3 function (with the input being a string) which loops through strings and takes paths that looks like this:
D:the\string\in\this\path\fix.jpg
C:hi\every\one.jpg
and changes it to this:
D:the\string\in\this\path/fix.jpg
C:hi\every/one.
so,...it is replace the LAST
'\'
with a'/'
I thought: "Nice, easy and quite straightforward. The price of $75 would be an overkill."
Catch-22 (Python Escaped Characters)
On my first try at manipulating the testing strings, everything went well with the second string. The first one gave me a big headache. Why? Becuse \f
is a special escaped character in Python. And, come to think of it, so is \n
, \t
, ... Even backslash itself is an escaping characters' indicator, so Python would need \\
to interpret it as a single \
in a string.
Code | Result |
---|---|
\' | Single Quote |
\\ | Backslash |
\n | New Line |
\r | Carriage Return |
\t | Tab |
\b | Backspace |
\f | Form Feed |
\ooo | Octal value |
\xhh | Hex value |
Detail from w3schools |
The Importance of Being Ernest Raw
After some time on StackOverflow, a triumph. The solution is a row string. If you prefix a string with the letter r
or R
, that string becomes a raw string. In a row string, backslashes \
are considered literal characters. They can be quite useful when you deal with regular expressions or Windows directory paths.
- Code using normal string:
print('D:the\string\in\this\path\fix.jpg')
- Output:
D:the\string\in his\path ix.jpg
- Code using raw string:
print(r'D:the\string\in\this\path\fix.jpg')
- Output:
D:the\string\in\this\path\fix.jpg
The Metamorphosis
There are a few ways how to work with raw files. Firstly, the input()
function takes a raw string value. That's nice.
The second way is, as shown earlier, to add r
at the beginning of a string.
The third way is related to the reading of a file. By default, the written backslashes are considered literally.
The fourth way is a command line argument input. In this case, the arguments that follow the execution of the script should be in quotes in order to be considered raw string values.
python script.py "C:\code\institute\file.txt"
Great Expectations (How Did I Solve It)
So, in the end, I proposed a solution that could work with a list of Windows paths hard-coded, or taken from a file, or taken from the command line arguments.
Here's the code.
import sys def change_backslash(string): if '\\' not in string: print('ERROR: Cannot change anything. There\'s no backslash!') sys.exit(1) else: list_string = [char for char in string] all_occurences = [i for i, char in enumerate(list_string) if char == '\\'] list_string[all_occurences[-1]] = '/' return ''.join(list_string) # from a list test_list = [r'D:the\string\in\this\path\fix.jpg', r'C:hi\every\one.jpg'] for item in test_list: print(change_backslash(item)) # from a file with open('files.txt') as f: for line in f.read().split('\n'): if not line: continue print(change_backslash(line)) # from the cli arguments if len(sys.argv) > 1: for i in range(1,len(sys.argv)): print(change_backslash(sys.argv[i]))
Here's the content of the file files.txt
:
D:the\string\in\this\path\fix.jpg C:hi\every\one.jpg
In the end, I would like to ask forgiveness from the famous authors from whose great books I have taken the titles for this article.