To extend Václav’s reply, Jamaine, if backslash ( ’ \’ ) is one of the character in a string, the default meaning of the backslash is “the following character is a control character” where a control character is a special code to make the interpreter do something more than just display a character in a string. You may have already seen ’ \n ’ used to trigger a Line Feed (go to next line) in a print()
instruction like
print("first line \n second line \n third line")
So with source = Path('C:\Users')
Python’s interpreter expects that ‘U’ is a control code. However, there is no such control code, so the error message is telling you that ‘\U
’ can’t be decoded as a control code. If you happen to follow the backslash with a valid control code, this can lead to some unexpected behavior like when you run something like print("this\next week")
.
To use the backslash as a character in a string, you have to reassert the backslash so Python knows that ‘\\
’ means “this is only a backslash, not a control code”. So your system paths in Python will look like ‘C:\\Users\\...
’
BTW, this “escape character” is found in many programming languages. In fact, this forum platform (Discourse) uses it. Here’s a useful example: When you paste code here, you should “fence” it with three backticks ( ``` ) as the first and last lines. Since three backticks are a control code to trigger the monotext font
, I had to precede the backticks in the previous sentence with a backslash to “escape out” of the default behavior in order to just print the backticks.
The ``` fence also has other useful features like preserving indents and suppressing curved quotation marks. So next time you paste code, it will look like this in your raw post text:
```python
<your code here>
```
The ‘python’ on the first line activates color coding so that the code components are easier to track.
source_folder = Path('C:\Users\JB095808\...')
P.S. These text format control codes are called “Markdown”. (@epicwink abbreviated it as ‘md’, in case you didn’t recognize this abbreviation). A full treatment of the Commonmark markdown used by Discourse (and other platforms, like WhatsApp) CAN BE FOUND HERE. AND ALSO HERE