Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中打开具有中文名称的文件_Python - Fatal编程技术网

如何在python中打开具有中文名称的文件

如何在python中打开具有中文名称的文件,python,Python,我试图用python中的“open()”函数以“w”模式打开一个文件 文件名为:仿宋人笔意.jpg 打开函数使用此文件名失败,但使用普通文件成功 在python中,如何打开名称不是英文的文件 我的代码如下: try: filename = urllib.quote(filename.encode('utf-8')) destination = open(filename, 'w') yield("<br>Obtained the file reference"

我试图用python中的“open()”函数以“w”模式打开一个文件

文件名为:仿宋人笔意.jpg

打开函数使用此文件名失败,但使用普通文件成功

在python中,如何打开名称不是英文的文件

我的代码如下:

try:
    filename = urllib.quote(filename.encode('utf-8'))
    destination = open(filename, 'w')
    yield("<br>Obtained the file reference")
except:
    yield("<br>Error while opening the file")
试试看:
filename=urllib.quote(filename.encode('utf-8'))
目标=打开(文件名“w”)
收益率(
获得了文件引用) 除: 产量(“打开文件时出错”)
对于非英语文件名,我总是得到“打开文件时出错”


提前感谢。

如果您提供文件名,它看起来正确吗?我不确定filname在到达此代码段之前是否已损坏

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import codecs
f=codecs.open(u'仿宋人笔意.txt','r','utf-8')
print f.read()
f.close()
在这里工作得很好

这个:

filename.encode('utf-8') 

尝试将文件名从
ascii
编码转换为
utf-8
,并导致错误。

如果您遇到问题,可能与您的操作系统或终端配置有关,而不是与Python本身有关;即使不使用编解码器模块,它对我来说也正常工作

以下是一个测试的shell日志,该测试打开了一个图像文件,并用您提供的中文名称将其复制到一个新文件中:

$ ls
create_file_with_chinese_name.py    some_image.png
$ cat create_file_with_chinese_name.py 
#!/usr/bin/python
# -*- coding: UTF-8 -*-

chinese_name_file = open(u'仿宋人笔意.png','wb')

image_data = open('some_image.png', 'rb').read()

chinese_name_file.write(image_data)

chinese_name_file.close()
$ python create_file_with_chinese_name.py 
$ ls
create_file_with_chinese_name.py    some_image.png              仿宋人笔意.png
$ diff some_image.png 仿宋人笔意.png 
$ 

为我工作,图像是相同的。

我尝试修改代码并将其重写为:

    destination = open(filename.encode("utf-8"), 'wb+')
    try:
        for chunk in f.chunks():
                destination.write(chunk)
        destination.close()
    except os.error:
        yield( "Error in Writing the File ",f.name)
它解决了我的错误


谢谢大家花时间回答。我没有尝试上面提到的选项,因为我能够修复它,但谢谢大家。

您尝试过codecs.open()?@Robus:没有,我没有尝试codecs.open()。知道怎么用吗。我可以用谷歌搜索,但如果你能回答的话,那会节省我的时间。但是mahendraliya问的是创建一个文件,而不是读取一个现有的文件。你所要做的就是用
编解码器的编码参数替换r。open
指的是文件内容的编码,而不是文件名本身的编码
utf-8
在这里是不相关的,因为我们讨论的是图像文件。