Search code examples
pythonpopen

New line character appended to os.popen().read() command


I am retrieving a file through the below command:

fileName = os.popen('ls -t testfile.txt |head -n1').read()

while printing fileName, I see \n appended.

I know we can remove \n through replace command, but I want to know why this is happening.


Solution

  • It is the ls command that append a new-line character to the output, you can figure it out opening a terminal and executing the command you want to run by popen