I need to put a lot of filepaths in the form of strings in Python as part of my program. For example one of my directories is D:\ful_automate\dl
. But Python recognizes some of the characters together as other characters and throws an error. In the example the error is IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\x0cul_automate\\dl
. It happens a lot for me and every time I need to change the directory name to one that may not be problematic.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Use raw string instead of string ie use r'filepath' It fixes the problem off blacklash "\"
The
\
character is used to form character escapes;\f
has special meaning.Use
/
or use raw stringr''
instead. Alternatively, you could ensure that Python reads the backslash as a backslash by escaping it with an additional\
.Demo to show the difference: