简体   繁体   中英

Can you use os.path.exists() on a file that starts with a number?

I have a set of files named 16ID_#.txt where # represents a number. I want to check if a specific file number exists, using os.path.exists() , before attempting to import the file to python. When I put together my variable for the folder where the files are, with the name of the file (ex: folderpath+"\\16ID_#.txt" ), python interprets the "\\16" as a music note.

Is there any way I can prevent this, so that folderpath+"\\16ID_#.txt" is interpreted as I want it to be?

I cannot change the names of the files, they are output by another program over which I have no control.

无论使用什么操作系统,都可以使用/来构建路径,但是正确的方法是使用os.path.join

os.path.exists(os.path.join(folderpath, "16ID_#.txt"))

I get these are windows \\paths. Maybe the problem is that you need to escape the backslash, because \\16 could be interpreted as a special code. So maybe you need to put \\\\16 instead of \\16 .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM