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

查看:468
本文介绍了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?

推荐答案

路径中的空格没有问题,因为您没有使用外壳"来打开文件.这是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:\Users\Gnibbler>dir "C:\ABC\SEM 2\testfiles"
 Volume in drive C has no label.
 Volume Serial Number is 46A0-BB64

 Directory of c:\ABC\SEM 2\testfiles

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:\Users\Gnibbler>

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

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