I get error on this line when I run code in Linux:
dst_file_path = "%s\%s" % (dst,loc[1])
How can do this using some module. Thanks in advance.
os.path.join
will automatically include the correct directory separator based on the platform being used.
import os
dst_file_path = os.path.join(dst, loc[1])
dst_file_path = "%s\\%s" % (dst,loc[1])
In strings, the "\\" character is used to escape the standard meanings of other characters. For example, a newline character, which we type as "Enter," is seen in Python as \\n
The backslash in your code is being read as a way to escape the usual meaning of the next character. If you just want a backslash, you have to escape the meaning of the backslash character. Ironically, this is done with another backslash: \\\\
To have Python read your strings as you would, which nullifies backslash benefits and string formatting things with %
, you can put an r
in front of your string, identifying it as a raw string.
or alternatively
dst_file_path = r"%s\%s" % (dst,loc[1])
if you're not making a filepath for the current file system, you can use the specific underlying module of os.path
. For backslashes windows style
import ntpath
dst_file_path = ntpath.join(dst, loc[1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.