1

我有一个由cron. 该脚本导入 pandas 模块并用于read_csv将 csv 加载到数据框,然后将其保存到另一个 csv。'apath' 是文件的绝对路径:

statedata_raw=pd.read_csv(apath+'statedata.csv')
statedata_raw.to_csv(apath+'state_data.csv',index=False)    

csv文件的权限设置正确-rwxr-xr-x

当我在命令行中运行它时,一切正常。当我通过 cron 运行它时,出现以下错误:

Traceback (most recent call last):
  File "/users/maderman/wdtest.py", line 21, in <module>
    statedata_raw=pd.read_csv(apath+'statedata.csv')
  File "/opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 448, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "/opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 880, in __init__
    self._make_engine(self.engine)
  File "/opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1114, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1891, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 374, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 678, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Initializing from file failed

我验证了 pandas 本身正在加载,并且to_csv通过替换read_csv. 当我用read_csv以下代码替换以手动创建数据框时,一切正常,在命令行中运行并在 cron 中运行:

cat=['a','a','a','a','a','b','b','b','b','b']
val=[1,2,3,4,5,6,7,8,9,10]
columns=['cat','val']
data=[cat,val]
dict={key:value for key,value in zip(columns,data)}
statedata_raw=pd.DataFrame(data=dict)

我发现另一个帖子建议将参数传递engine='python'read_csv,但这没有做任何事情。

所以我知道:

  1. cron 运行 python 很好
  2. 它可以导入 pandas 并运行几个不同的 pandas 函数。
  3. 文件权限很好

该问题似乎与命令特别相关read_csv

任何建议,将不胜感激。

4

1 回答 1

0

这个问题的框架是错误的,它归结为权限问题。在这里发布并回答了一个更好的问题:stackoverflow.com/questions/62353610

于 2020-06-22T15:01:53.897 回答