A
A
Alexey2021-07-26 15:18:46
Python
Alexey, 2021-07-26 15:18:46

Why does coding in a python script work in the terminal, but not in cron?

Python 2.7
In the terminal, the script works fine, a file with a name in Cyrillic is created on the disk.
I run it in cron - it gives an error:

from io import open

def cache_set(key, val, path=None):
    if path is None:
        path = u'%s/cache' % os.getcwd()

    if isinstance(key, str):
        key = key.decode('utf8')

    if isinstance(val, str):
        val = val.decode('utf8')

    elif not isinstance(val, unicode):
        val = str(val).decode('utf8')

    with open(u'{0}/{1}'.format(path, key), 'w', encoding="utf-8") as f:
        f.write(val)

    return True

key = u"Пермь"
val = u"..."
cache_set(key, val)

Traceback (most recent call last):
...
    with open(u'{0}/{1}'.format(path, key), 'w', encoding="utf-8") as f:
IOError: [Errno 2] No such file or directory: '/home/master/cache/\xd0\x9f\xd0\xb5\xd1\x80\xd0\xbc\xd1\x8c'


In crontab:
PYTHONIOENCODING=utf8

What's wrong? And after all, it does not give an encoding / decoding error, but generates an encoded name.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-27
@Vindicar

1. Put assert before with open(), make sure both path and key are unicode strings.
2. Are you running in the cron under your own user? Maybe it's about rights?
3. Do you need python 2? In 3m somehow easier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question