英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论