Newline characters generated in a Python script are not accepted in another job

I have a specific application combination in which a certain part of it takes a bunch of parameters from a file. Everything I use is based on Linux.

The problem is that something in my work tube does not like the newline character. For one thing, I am writing a script as follows:

with open(job_script_file, 'w') as f:
     f.write("parameter 1 goes here\n")
     f.write("parameter 2 goes here\n")
     f.write("python script call plus arguments here")

However, these arguments are rejected by the task manager unless I manually edit the script task before starting it. If I manually scramble new lines with some changes, it will work. If I do not edit manually at all (directly from the python script), it does not work.

Are there any suggestions as to why this newline character (or maybe something else) is not accepted / read correctly using the csh script job?


I tried using sed -i to edit the file in place and add an empty line before calling python:

sed -i 's/^python/\n\python/g'

While this file was being edited, the problem was not resolved.

UPDATE: as indicated in the comments, the hexdump head / tail of the file that worked (after manually editing the file):

od -xc <working_file> | head -15
0000000    2123    622f    6e69    632f    6873    2d20    0a66    5323
          #   !   /   b   i   n   /   c   s   h       -   f  \n   #   S
0000020    4142    4354    2048    2d2d    6f6a    2d62    616e    656d
          B   A   T   C   H       -   -   j   o   b   -   n   a   m   e
0000040    573d    3056    5f31    3032    3431    3730    3931    315f
          =   W   V   0   1   _   2   0   1   4   0   7   1   9   _   1
0000060    3230    3030    3031    3230    3946    3344    3045    5f30
          0   2   0   0   1   0   0   2   F   9   D   3   E   0   0   _
0000100    3031    3032    3130    3030    3133    3634    3130    3030
          1   0   2   0   0   1   0   0   3   1   4   6   0   1   0   0
0000120    322d    3130    3037    3033    2d36    6574    7473    6a5f
          -   2   0   1   7   0   3   0   6   -   t   e   s   t   _   j
0000140    626f    230a    4253    5441    4843    2d20    6e2d    646f
          o   b  \n   #   S   B   A   T   C   H       -   -   n   o   d
0000160    7365    313d    230a    4253    5441    4843    2d20    632d

od -xc <working_file> | tail -10
          i   n   :   :   1   0   2   0   0   1   0   0   3   1   4   6
0007140    3130    3030    3520    3030    3733    3238    3432    3231
          0   1   0   0       5   0   0   3   7   8   2   2   4   1   2
0007160    3a30    6a3a    696f    3a6e    353a    3030    3733    3339
          0   :   :   j   o   i   n   :   :   5   0   0   3   7   9   3
0007200    3337    3131    2030    3222    3130    2d34    3730    312d
          7   3   1   1   0       "   2   0   1   4   -   0   7   -   1
0007220    2239    000a
          9   "  \n
0007223

and the same for a broken file:

od -xc <non-working_file> | head -15
0000000    2123    622f    6e69    632f    6873    2d20    0a66    5323
          #   !   /   b   i   n   /   c   s   h       -   f  \n   #   S
0000020    4142    4354    2048    2d2d    6f6a    2d62    616e    656d
          B   A   T   C   H       -   -   j   o   b   -   n   a   m   e
0000040    573d    3056    5f31    3032    3431    3630    3731    315f
          =   W   V   0   1   _   2   0   1   4   0   6   1   7   _   1
0000060    3230    3030    3031    3230    3844    4644    3035    5f30
          0   2   0   0   1   0   0   2   D   8   D   F   5   0   0   _
0000100    3031    3032    3130    3030    3233    3241    3132    3030
          1   0   2   0   0   1   0   0   3   2   A   2   2   1   0   0
0000120    322d    3130    3037    3033    2d36    6574    7473    6a5f
          -   2   0   1   7   0   3   0   6   -   t   e   s   t   _   j
0000140    626f    230a    4253    5441    4843    2d20    6e2d    646f
          o   b  \n   #   S   B   A   T   C   H       -   -   n   o   d
0000160    7365    313d    230a    4253    5441    4843    2d20    632d


od -xc <non-working_file> | tail -10
          n   :   :   1   0   2   0   0   1   0   0   3   2   A   2   2
0007140    3031    2030    3035    3330    3536    3037    3136    3038
          1   0   0       5   0   0   3   6   5   7   0   6   1   8   0
0007160    3a3a    6f6a    6e69    3a3a    3035    3330    3837    3833
          :   :   j   o   i   n   :   :   5   0   0   3   7   8   3   8
0007200    3134    3035    2220    3032    3431    302d    2d36    3731
          4   1   5   0       "   2   0   1   4   -   0   6   -   1   7
0007220    0022
          "
0007221
+4
source share
3 answers

: ( \n ). , :

with open(job_script_file, 'w') as f:
     f.write("parameter 1 goes here\n")
     f.write("parameter 2 goes here\n")
     f.write("python script call plus arguments here\n")  # \n to cleanly end the command

, . , , .

+2

, :

from os import linesep

with open("your_script_file", 'w') as f:
    f.write("your_data" + linesep)
    f.write("your_data_too" + linesep)

: os.linesep

:

os.linesep ( ); '\n' .

+1

If the native Python solutions do not work, first try the following:

with open(job_script_file, 'w') as f:
     f.write("parameter 1 goes here@")
     f.write("parameter 2 goes here@")
     f.write("python script call plus arguments here")

And then:

tr '@' '\n' < file.txt

If you can take for granted that the @ symbol will not be present in the parameters. Also try \ r instead of \ n and see what happens.

Otherwise, you can use xdotool to automatically perform manual changes for you. ( documentation )

0
source

Source: https://habr.com/ru/post/1671896/


All Articles