0

我一直在尝试将桌面设置为我的工作目录,所以我可以加载一个 csv

import os
path="/Users/HOME/Desktop"
os.getcwd()

它返回 /

使用熊猫库我没有使用

DF =  pd.read_csv("filename", "mode")
4

1 回答 1

0

You did not actually change the working directory, you merely assigned a path to a variable. You need to invoke os.chdir() with that path (see https://stackoverflow.com/a/431715/14015737):

import os
path="/Users/HOME/Desktop"
os.chdir(path)
os.getcwd()

This should return the path.

In order to then read your .csv file that is located there (e.g. /Users/HOME/Desktop/test.csv) you can call read_csv() without a path. Full example:

import os
import pandas 

path='/Users/HOME/Desktop'
os.chdir(path)

df = pandas.read_csv('test.csv')
于 2021-01-19T13:32:58.260 回答