简体   繁体   English

Python文件夹名称

[英]Python Folder Name

I have a directory in my computer which is having a numeral as the folder name.. For Example, the directory that exist is 我的计算机中有一个目录,其中的文件夹名称为数字。例如,存在的目录为

C:\\3_Software C:\\ 3_软件

I am trying to provide the top level directory path.. I want to look into folders, subfolders and files from C:\\3_Software, open the file and search for a string. 我正在尝试提供顶级目录路径。.我想从C:\\ 3_Software中查找文件夹,子文件夹和文件,打开文件并搜索字符串。 If the specific string exist, then print the file name and the string. 如果存在特定的字符串,则打印文件名和字符串。 So I used the os.walk to iterate over the folders, subfolders and files. 因此,我使用os.walk遍历了文件夹,子文件夹和文件。 Now when it is fetching the directory name, since it has a numeral in the folder name, it is automatically converting to a different format and unable to fetch the file from there. 现在,当它获取目录名称时,由于它在文件夹名称中包含数字,因此它将自动转换为其他格式,并且无法从此处获取文件。

When i tried to define the 'C:\\3_Software' as root and read back the same variable, it is giving me as 'C:\\x03_Software'.... 当我尝试将'C:\\ 3_Software'定义为root并回读相同的变量时,它给我的名字是'C:\\ x03_Software'。

And, is there any means of concatenating the root and file path... using the os.walk method.. 并且,是否有使用os.walk方法连接根目录和文件路径的任何方法。

Escape the backslash: 转义反斜杠:

'C:\\3_Software'

or make it a raw string: 或将其设为原始字符串:

r'C:\3_Software'

As your code currently stands, you are trying to use character "\\3": 如您当前的代码所示,您正在尝试使用字符“ \\ 3”:

>>> '\3'
'\x03'
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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