Python - 程序不断迭代已删除的文件

时间:2018-01-01 16:10:41

标签: python

我编写了一个程序,它迭代目录中的所有CSV文件,并根据其内容创建一个新的CSV文件。

我编写了一个执行这些任务的函数('summary()'),并由以下代码调用

cwd = os.getcwd()
csv_list = []

for root, dirs, filenames in os.walk(cwd):
    for f in filenames:
       if f.endswith('.csv'):
            csv_list.append(f)

#for root, dirs, filenames in os.walk(cwd):
summary(csv_list)

将文件加载到函数中后,通过以下代码将其添加到pandas DF中

df = pd.concat((pd.read_csv(f, parse_dates=True, sep=';') for f in files))

该函数创建一个名为'combined_csv'的输出csvfile。 我在每次运行之间删除此文件(因为我目前正在测试程序)。

然而,我一直遇到以下特殊的错误。

FileNotFoundError: File 'combined.csv' does not exist

即使我删除了该文件,程序仍会对其进行解析 - (当它尝试加载时崩溃)。为什么呢?我删除文件后重新启动程序,该文件根本不应出现在'csv_list'变量中。

信息是否以某种方式缓存?

我在下面添加了完整的回溯。

    Traceback (most recent call last):
  File "summary.py", line 112, in <module>
    summary(csv_list)
  File "summary.py", line 17, in summary
    df = pd.concat((pd.read_csv(f, parse_dates=True, sep=';') for f in files))
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/reshape/concat.py", line 206, in concat
    copy=copy)
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/reshape/concat.py", line 236, in __init__
    objs = list(objs)
  File "summary.py", line 17, in <genexpr>
    df = pd.concat((pd.read_csv(f, parse_dates=True, sep=';') for f in files))
  File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 655, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 405, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 764, in __init__
    self._make_engine(self.engine)
  File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 985, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 1605, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 394, in pandas._libs.parsers.TextReader.__cinit__ (pandas/_libs/parsers.c:4209)
  File "pandas/_libs/parsers.pyx", line 710, in pandas._libs.parsers.TextReader._setup_parser_source (pandas/_libs/parsers.c:8873)
FileNotFoundError: File b'combined.csv' does not exist

编辑我简化了程序(代码与问题无关,并将代码更改为此。这是所有运行的代码。

我正在从位于目录中的终端(Ubuntu 16.04)执行程序。

$ pwd

返回

/home/jasper/PycharmProjects/AHP_Scanner/PVM/true_run/testsum

$ ls -a /home/jasper/PycharmProjects/AHP_Scanner/PVM/true_run/testsum

返回:

    .            fixed_10.csv  fixed_13.csv  fixed_16.csv  fixed_19.csv  fixed_21.csv  fixed_4.csv  fixed_7.csv  goed
..           fixed_11.csv  fixed_14.csv  fixed_17.csv  fixed_1.csv   fixed_2.csv   fixed_5.csv  fixed_8.csv  summary.py
fixed_0.csv  fixed_12.csv  fixed_15.csv  fixed_18.csv  fixed_20.csv  fixed_3.csv   fixed_6.csv  fixed_9.csv

我们可以看到,'combined_csv'文件不存在

然而,当我运行以下代码时:(这是所有运行的代码,其余的summary.py已被注释掉)

cwd = os.getcwd()
csv_list = []
for root, dirs, filenames in os.walk(cwd):
    for f in filenames:
        if f.endswith('.csv'):
            print(f)

我收到了这个回复:

fixed_8.csv
fixed_10.csv
fixed_4.csv
fixed_11.csv
fixed_9.csv
fixed_7.csv
fixed_0.csv
fixed_12.csv
fixed_2.csv
fixed_5.csv
fixed_20.csv
fixed_18.csv
fixed_14.csv
fixed_6.csv
fixed_15.csv
fixed_3.csv
fixed_1.csv
fixed_17.csv
fixed_13.csv
fixed_19.csv
fixed_16.csv
fixed_21.csv
combined.csv

我不知道为什么这个文件不断出现。

0 个答案:

没有答案