Python寻址CSV文件中Linux与Windows的区别

2024-10-01 04:45:47 发布

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

我正在尝试从Windows迁移到Linux,我的一些Python代码在迁移过程中没有幸存下来。你知道吗

这是Windows版本:

path = r'C:\\Users\\x\\PythonTestFiles\\TestFile.csv'
file = open(path, newline='')
TestFileRaw = csv.reader(file)

header = next(TestFileRaw)
dataPC = [row for row in TestFileRaw]

等等。。。。你知道吗

我试图在某个目录中对csv文件进行寻址,但我搞不清Linux的寻址方式。你知道吗

有人能帮忙吗?你知道吗


Tags: csvpath代码版本过程linuxwindowsnewline
3条回答

下面是一些关于linux directory structure的信息

在文件上按Alt+Enter,然后在“属性”对话框中将路径复制到其根目录,例如/home/<user_name>/Documents,然后用csv文件的file name附加该路径。你知道吗

最后,csv文件的路径变成

/home/<user_name>/Documents/filename.csv

在linux中使用csv文件的任何路径!你知道吗

path = r'/home/x/python_test_files/test_file.csv'
file = open(path, newline='')
TestFileRaw = csv.reader(file)

header = next(TestFileRaw)
dataPC = [row for row in TestFileRaw]

您可以使用来自os模块的分隔符:

import os
path = os.path.join('your_folder_name', 'your_file_name')

这样它就可以独立于Windows/Linux。你知道吗

这是official documentation to os.path module。你知道吗

相关问题 更多 >