windows文件路径中的空格 [英] whitespaces in the path of windows filepath

查看:50
本文介绍了windows文件路径中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 进行文件操作.

I am working on file operations using python.

我有一个文件路径:

filepath = "E:/ABC/SEM 2/testfiles/all.txt"

当我使用 python 打开文件时,它说:

when I am opening the file using python, it says me :

IOError: No such file:

但是,该文件存在于驱动器上.
这可能是因为 Windows 无法正确使用SEM 2",因为它包含空间.
窗口路径路径中出现这样的空格怎么处理?

but, the file is present on the drive.
It may be because windows cannnot take "SEM 2" properly as it contains space.
How can I deal with such whitespaces in the path of window path?

推荐答案

路径中的空格没有问题,因为您没有使用shell"来打开文件.这是来自 Windows 控制台的一个会话来证明这一点.你做错了其他事情

There is no problem with whitespaces in the path since you're not using the "shell" to open the file. Here is a session from the windows console to prove the point. You're doing something else wrong

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> os.makedirs("C:/ABC/SEM 2/testfiles")
>>> open("C:/ABC/SEM 2/testfiles/all.txt","w")
<open file 'C:/ABC/SEM 2/testfiles/all.txt', mode 'w' at 0x0000000001D95420>
>>> exit()

C:UsersGnibbler>dir "C:ABCSEM 2	estfiles"
 Volume in drive C has no label.
 Volume Serial Number is 46A0-BB64

 Directory of c:ABCSEM 2	estfiles

13/02/2013  10:20 PM    <DIR>          .
13/02/2013  10:20 PM    <DIR>          ..
13/02/2013  10:20 PM                 0 all.txt
               1 File(s)              0 bytes
               2 Dir(s)  78,929,309,696 bytes free

C:UsersGnibbler>

这篇关于windows文件路径中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆