[英]How to import several csv files from a folder, within a multiplatform environment
[英]How do I import data from n number of csv files within a folder?
[蟒蛇幫助]
您好,我需要一些幫助來弄清楚如何從一個文件夾中的 n 個文件導入數據。 當我嘗試提取實際數據時,我收到一條錯誤消息,指出該文件/目錄不存在。
import os, csv
path = ("my directory")
files = sorted(os.listdir(path))
def f():
for file in (files):
with open(file, 'r') as csvfile:
data = csvfile.read
print(data)
輸出:
File "<ipython-input-158-1f4c11da5a68>", line 1, in <module>
f()
File "<ipython-input-157-b977510dbfcd>", line 8, in f
with open(file, 'r') as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: '.csv'
os.listdir
只返回文件名,沒有它們的路徑名,所以你需要在打開它們時將路徑名與文件名連接起來。
改變:
with open(file, 'r') as csvfile:
到:
with open(os.path.join(fcm_path, file), 'r') as csvfile:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.