cron中的python脚本不会读取CSV,除非它自己创建了CSV [英] python script in cron not reading a CSV unless it creates the CSV itself

查看:113
本文介绍了cron中的python脚本不会读取CSV,除非它自己创建了CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本。



变量'apath'是文件的绝对路径。



p>

  cat = ['a','a','a','a','a','b',' b','b','b','b'] 
val = [1,2,3,4,5,6,7,8,9,10]
列= [' cat','val']
data = [cat,val]
dict = {key:键的值,zip中的值(列,数据)}

statedata_raw = pd.DataFrame(data = dict)
statedata_raw.to_csv(apath +'state_data.csv',index = False)

statedata_raw2 = pd.read_csv(apath +'state_data.csv')
statedata_raw2.to_csv(apath +'state_data2.csv',index = False)

但是当我尝试时要手动运行第一部分,创建第一个csv,然后通过cron运行第二部分,第二个read_csv语句将失败。我检查了state_data.csv文件的权限,它们很好。它设置为 -rwxr-xr-x



具体来说:我首先通过命令行手动运行此脚本。它执行并创建state_data.csv。然后我检查state_csv的权限,它们是 -rwxr-xr-x

  cat = ['a','a','a','a','a','b','b','b','b','b'] 
val = [1,2,3,4,5,6,7,8,9,10]
列= ['cat','val']
data = [cat,val]
dict = {key:密钥的值,zip中的值(列,数据)}

statedata_raw = pd.DataFrame(data = dict)
statedata_raw.to_csv(apath +' state_data.csv',index = False)

然后通过cron执行此脚本,该脚本失败并给出以下错误消息

  statedata_raw2 = pd.read_csv(apath +'state_data.csv')
statedata_raw2.to_csv(apath + 'state_data2.csv',index = False)

这是我从系统中得到的错误

 回溯(最近一次通话为最后一次):
文件 /users/michaelmader/wdtest.py,第39行,在< module>中
statedata_raw2 = pd.read_csv(apath +'state_data.csv')
文件 /opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py,行676,在parser_f
中,返回_read(filepath_or_buffer,kwds)
文件 /opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py,行448,在_read $ b中$ b解析器= TextFileReader(fp_or_buf,** kwds)
文件 /opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py,第880行,位于__init__
self._make_engine(self.engine)
文件 /opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py,行1114,位于_make_engine
self中。 _engine = CParserWrapper(self.f,** self.options)
文件 /opt/miniconda3/lib/python3.7/site-packages/pandas/io/parsers.py,第189行,位于__init__
self._reader = parsers.TextReader(src,** kwds)
文件 pandas / _libs / parsers.pyx,第374行,在pandas._libs.parsers.TextReader .__ cinit__
文件pandas._libs.parsers中的 pandas / _libs / parsers.pyx,第678行.TextReader._setup_parser_source
OSError:从文件初始化失败

总结


  1. 通过终端运行完整脚本:state_data2.csv已创建:pass

  2. 通过cron运行完整脚本:state_data2.csv已创建:pass

  3. 通过终端运行第一部分,通过cron运行第二部分:失败

我在MacOS上,并且已经在系统偏好设置中为crontab授予了完整的磁盘访问权限。

解决方案

我已解决了问题。问题是在MacOS中授予cron的权限。我以为我已经通过授予 \usr\bin\crontab 完整的磁盘访问权限来解决此问题,但实际上我需要为授予完全的磁盘访问权限usr\sbin\cron



执行此操作的步骤可以在以下位置找到: https://blog.bejarano.io/fixing-cron-jobs-in-mojave/



一旦做出更改,一切都会正常进行。


I have the following script. It works when I run it in command line, and it works when I run it in cron.

The variable 'apath' is the absolute path of the file.

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)
statedata_raw.to_csv(apath+'state_data.csv',index=False)

statedata_raw2=pd.read_csv(apath+'state_data.csv')
statedata_raw2.to_csv(apath+'state_data2.csv',index=False)

But when I try to run the first part manually, creating the first csv, and then run the second part through cron, the second read_csv statement fails. I checked the permissions on the state_data.csv file and they are fine. It's set to -rwxr-xr-x

To be specific: I first run this script manually through command line. It executes and creates state_data.csv. Then I check the permissions of state_csv, and they are -rwxr-xr-x

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)
statedata_raw.to_csv(apath+'state_data.csv',index=False)

and then this script via cron, which fails, and gives the error message below

statedata_raw2=pd.read_csv(apath+'state_data.csv')
statedata_raw2.to_csv(apath+'state_data2.csv',index=False)

This is the error that I get from the system

Traceback (most recent call last):
  File "/users/michaelmader/wdtest.py", line 39, in <module>
    statedata_raw2=pd.read_csv(apath+'state_data.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

To summarize

  1. Run complete script through Terminal: state_data2.csv is created: pass
  2. Run complete script through cron: state_data2.csv is created: pass
  3. Run first part through Terminal, second part through cron: fail

I am on MacOS and I already gave crontab full disk access in system preferences.

解决方案

I figured out the problem. The issue was the permissions that were granted to cron in MacOS. I thought I had solved it by giving \usr\bin\crontab full disk access, but I actually needed to give full disk access to usr\sbin\cron

The steps for doing this can be found here: https://blog.bejarano.io/fixing-cron-jobs-in-mojave/.

Once I made that change everything worked fine.

这篇关于cron中的python脚本不会读取CSV,除非它自己创建了CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆