Skip to content
Advertisement

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement