Dos2unix does not work when trying to disable a command

I called dos2unix from Python this way:

call("dos2unix " + file1, shell=True, stdout=PIPE)

However, to disable Unix output, I did the following:

f_null = open(os.devnull, 'w')
call("dos2unix " + file1, shell=True, stdout=f_null , stderr=subprocess.STDOUT)

This does not work. The command is no longer called, since the diff that I execute on file1in the relation file2(did diff -y file1 file2 | cat -tand could see that the end of the line has not changed).

file2is the file I'm comparing with file1. It has a Unix line ending as it is generated on a box. However, it is likely file1not.

+4
source share
1 answer

Not sure why, but I would try to get rid of the “noise” around your command and check the return code:

check_call(["dos2unix",file1], stdout=f_null , stderr=subprocess.STDOUT)
  • , ( !)
  • shell=True, dos2unix
  • check_call,

, dos2unix , tty (dos2unix ). . , os.devnull , .

python- ( ), dos2unix ( Windows):

with open(file1,"rb") as f:
   contents = f.read().replace(b"\r\n",b"\n")
with open(file1+".bak","wb") as f:
   f.write(contents)
os.remove(file1)
os.rename(file1+".bak",file1)

, . ( ):

with open(file1,"rb") as fr, open(file1+".bak","wb") as fw:
   for l in fr:
      fw.write(l.replace(b"\r\n",b"\n"))
os.remove(file1)
os.rename(file1+".bak",file1)
+3

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


All Articles