Python无法关闭文件,Windows 10

时间:2019-05-22 12:52:29

标签: python python-3.x windows-10 jupyter-notebook

在Jupyter中读取.csv文件:

loads_with_query_in_progress

cmd错误消息:

filename = "myfile.csv"
start_pd = time.time()
try:
    with open (filename, 'rb') as file:
        reader = pd.read_csv(filename, chunksize=10000, error_bad_lines=False, header=None)
        df = pd.concat([x for x in reader], ignore_index=True)
        df.columns = dfcolslist
        file.close #also tried reader.close() and file.closed
    print("{} read successfully in {:.2f} secs".format(filename, time.time() - start_pd))
except IOError:
    print("could not read {}".format(filename))

GUI错误消息:

> ren "myfile.csv" "date_myfile.csv"
The process cannot access the file because it is being used by another process.

1 个答案:

答案 0 :(得分:2)

由于您要向pd.read_csv传递字符串,因此它将尝试打开一个已经打开的文件。

它的documentation

  

filepath_or_buffer:str,路径对象或类似文件的对象任何有效   字符串路径是可以接受的。该字符串可以是URL。有效网址   方案包括http,ftp,s3和file。对于文件URL,主机为   预期。本地文件可以是:file://localhost/path/to/table.csv。

     

如果要传递路径对象,pandas可以接受   pathlib.Path或py._path.local.LocalPath。

     

通过类似文件的对象,我们使用read()方法引用对象,例如   文件处理程序(例如,通过内置的打开功能)或StringIO。

要么:

  • 将文件名作为字符串传递给pd.read_csv,让它自行处理打开,读取和关闭文件的过程:

    reader = pd.read_csv(filename, chunksize=10000, error_bad_lines=False, header=None)
    
  • 或者,您可以自己使用with open打开文件,然后将文件对象传递给pd.read_csv

    with open (filename, 'rb') as file:
        reader = pd.read_csv(file, chunksize=10000, error_bad_lines=False, header=None)
    

在任何一种情况下,您都不应该自己拨打.close()