如何打开具有特殊字符(”é”)在其路径上的文件?

huangapple go评论64阅读模式
英文:

How to open a file that has special character ("é") on its path?

问题

我正在尝试访问一个包含特殊字符 "é" 的路径中的文件,如下所示:

import xarray as xr

file_path = "//business/Partage/city/Données/climate/amplitude_1950-01-01.nc"

data = xr.open_dataset(file_path)

我收到以下错误信息:

[Errno 2] No such file or directory: b'\\business\Partage\city\Donn\xc3\xa9es\climate\amplitude_1950-01-01.nc'

为了让Python读取文件,我尝试使用pathlib,或者将路径编写如下:file_path.encode('utf-8').decode('latin-1')。仍然收到相同的错误。

我正在使用Python 3.7在VS Code上工作。由于我正在使用服务器,无法更改路径中的任何字符。

英文:

I am trying to access a file that is in a path with a special character "é" as shown below

import xarray as xr

file_path = "//business/Partage/city/Données/climate/amplitude_1950-01-01.nc"

data = xr.open_dataset(file_path)

I am getting this following error:

> [Errno 2] No such file or directory: b'\\business\Partage\city\Donn\xc3\xa9es\climate\amplitude_1950-01-01.nc'

To get Python to read the file I have tried using pathlib, or writing my path as follows: file_path.encode('utf-8').decode('latin-1'). Still getting the same error.

I am using Python 3.7 on VS Code. As I am working on a server, I cannot change any character on the path.

答案1

得分: 1

尝试在路径中使用原始字符串(文件内容目前还不是问题):

file_path = r"//business/Partage/city/Données/climate/amplitude_1950-01-01.nc"
英文:

Try using a raw string for the path (the content of the file is not yet a problem):

file_path = r"//business/Partage/city/Données/climate/amplitude_1950-01-01.nc"

答案2

得分: 0

Python 使用 UTF-8 编码因此你甚至可以使用非标准的字母字符来命名你的变量遗憾的是在变量名中不能使用表情符号),如果你想激怒可能会阅读你的代码的人我相当肯定这违反了 PEP 规范所以你实际上不应该这样做)。读取 UTF-8 编码的文本文件稍微复杂一些但也没关系

    import io
    
    Ž = "👁 👐 👅"
    print(Ž)
    
    path = r'C:\Temp\Some file named with all my favourite characters ąčęėįšųū.txt'
    
    with io.open(path, mode="r", encoding="utf-8") as f:
        file = f.read()
    
    print(file)

文件内容

> 更多奇怪字符:˟̋ѬԿԪْᴥᵺ█▌░▒▓■♣♥♦
> 
> 让我们尝试表情符号:😄 😍 😇 😆 😄 😃 
> 
> 让我们绘制一个未完成的进度条与文本
> 
> ██████████░░░░░░░░░░

对我来说这在 Sublime 文本编辑器中可行因为它默认使用 UTF-8

[![enter image description here][1]][1]

但如果你尝试运行的脚本文件未使用 UTF-8 编码你将会遇到问题我怀疑这可能是这种情况下的问题确保你保存脚本的文件使用 UTF-8 编码
英文:

Python works with UTF-8 encoding so you can even use non standard letter characters to name your variables(no emojis in variable names unfortunately) if you want to trigger people who might read your code(I am pretty sure that's against PEP conventions so you shouldn't actually do that). Reading UTF-8 encoded text file is slightly more tricky, but that's also fine:

import io

Ž = "🤑 🤐 🤕"
print(Ž)

path = r'C:\Temp\Some file named with all my favourite characters ąčęėįšųū.txt'

with io.open(path, mode="r", encoding="utf-8") as f:
    file = f.read()

print(file)

Contents of my file:

> More weird characters: ˟̋ѬԿԪْᴥᵺ█▌░▒▓■♣♥♦
>
> Let's try emojis: 😄 😍 💗 🗾 🗼 🗻
>
> Let's draw a half done progress bar with text:
>
> ██████████░░░░░░░░░░

This works for me with Sublime text editor because it uses UTF-8 by default:

如何打开具有特殊字符(”é”)在其路径上的文件?

You will experience issues if the script file you are trying to run is not encoded UTF-8 though. I suspect that is the issue in this case. Make sure the file you save your script in uses UTF-8.

huangapple
  • 本文由 发表于 2023年7月17日 15:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定