第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

IOError:[Errno 2]沒有這樣的文件或目錄:但是文件在那里…

IOError:[Errno 2]沒有這樣的文件或目錄:但是文件在那里…

慕慕森 2021-04-29 06:07:38
如果您確實在下面的for循環(huán)#commented中打印了文件名,它將為您提供目錄中的所有文件名。但是,當我調(diào)用pd.ExcelFile(filename)時,它返回的文件名為:[第一個以'.xlsx'結尾的文件,我缺少什么?ps:下面的縮進是正確的,if在我的代碼中位于for之下,但此處未以這種方式顯示。for filename in os.listdir('/Users/ramikhoury/PycharmProjects/R/excel_files'):if filename.endswith(".xlsx"):    month = pd.ExcelFile(filename)    day_list = month.sheet_names    i = 0    for day in month.sheet_names:        df = pd.read_excel(month, sheet_name=day, skiprows=21)        df = df.iloc[:, 1:]        df = df[[df.columns[0], df.columns[4], df.columns[8]]]        df = df.iloc[1:16]        df['Date'] = day        df = df.set_index('Date')        day_list[i] = df        i += 1    month_frame = day_list[0]    x = 1    while x < len(day_list):        month_frame = pd.concat([month_frame, day_list[x]])        x += 1    print filename + ' created the following dataframe: \n'    print month_frame  # month_frame is the combination of the all the sheets inside the file in one dataframe !
查看完整描述

3 回答

?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

您的“ if”語句必須在for循環(huán)內(nèi)


查看完整回答
反對 回復 2021-05-25
?
白衣非少年

TA貢獻1155條經(jīng)驗 獲得超0個贊

問題是您試圖從與您列出的目錄不同的目錄中打開相對文件路徑。與其使用os它,不如使用一個更高級別的接口,例如pathlib:


import pathlib

for file_name in pathlib.Path("/Users/ramikhoury/PycharmProjects/R/excel_files").glob("*.xslx"):

    # this produces full paths for you to use

pathlib是在Python 3.4中添加的,因此,如果您使用的是舊版本的python,則最好的選擇是使用更老的glob模塊,該模塊的功能類似:


import glob

for file_name in glob.glob("/Users/ramikhoury/PycharmProjects/R/excel_files/*.xslx"):

    # this also produces full paths for you to use

如果出于某種原因您確實需要使用低級os接口,則解決此問題的最佳方法是使用對以下dir_fd參數(shù)的可選參數(shù)open:


# open the target directory

dir_fd = os.open("/Users/ramikhoury/PycharmProjects/R/excel_files", os.O_RDONLY)

try:

    # pass the open file descriptor to the os.listdir method

    for file_name in os.listdir(dir_fd):

        # you could replace this with fnmatch.fnmatch

        if file_name.endswith(".xlsx"):

            # use the open directory fd as the `dir_fd` argument

            # this opens file_name relative to your target directory

            with os.fdopen(os.open(file_name, os.O_RDONLY, dir_fd=dir_fd)) as file_:

                # do excel bits here

finally:

    # close the directory

    os.close(dir_fd)

盡管可以通過更改腳本頂部的目錄來完成此修復(如另一個答案所建議),但這具有更改進程的當前工作目錄的副作用,這通常是不希望的,并且可能會帶來負面影響。為了使這項工作沒有副作用,需要您chdir返回到原始目錄:


# store cwd

original_cwd = os.getcwd()

try:

    os.chdir("/Users/ramikhoury/PycharmProjects/R/excel_files")

    # do your listdir, etc

finally:

    os.chdir(original_cwd)

請注意,這會在您的代碼中引入競爭條件,original_cwd可能會被刪除,或者可能更改了該目錄的訪問控制,從而使您無法chdir返回到目錄,這正是dir_fd存在的原因。


dir_fd是在Python 3.3中添加的,因此,如果您使用的是舊版本的Python,我建議您僅使用glob而不是chdir解決方案。


查看完整回答
反對 回復 2021-05-25
  • 3 回答
  • 0 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號