与平台无关的文件路径numpy.genfromtx公司

2024-06-16 07:14:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在工作中使用numpy的genfromtxt函数。明天我要做一个关于如何使用Python的演示(这是我6周前才开始学习的东西)。问题是,我的许多听众将使用Mac或Linux系统。但是,我使用Windows。我找到了Path函数,但它不太起作用:

from pathlib import Path

Path('File_180620_123733_Aunp_10nm_stock.txt').resolve() 

返回:

C:\Users\brand\Downloads\Python\My Files\aupnipam_scan41_3DFLR(1).txt

对于windows,这也是我的操作系统,我需要文件路径的格式

C:\\Users\\brand\\Downloads\\Python\\My Files\\aupnipam_scan41_3DFLR(1).txt

genfromtxt工作。你知道吗

对于如何使这不仅适用于Windows,而且适用于所有操作系统,有什么建议吗?你知道吗


Tags: path函数numpytxtmywindowsmacdownloads
1条回答
网友
1楼 · 发布于 2024-06-16 07:14:44

你的公主在另一座城堡里。你知道吗

使用os.path.abspath('File_180620_123733_Aunp_10nm_stock.txt'),假设文件在当前工作目录中。Pathlib主要用于更复杂的路径操作。你知道吗

Since different operating systems have different path name conventions, there are several versions of this module in the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface.

https://docs.python.org/3/library/os.path.html#module-os.path

相关问题 更多 >