Problem phenomenon
The contents of file A are as follows:
View the contents of lines 2nd through 5th: Sed-n ' 2,5p '/tmp/test/a
Visible at the command line execution, there is no problem.
Using the Paramiko module in Python to execute the Linux command, the main code is as follows:
deftoserver (self, str): Sys.setdefaultencoding ('Utf-8') returnStr.decode ('Utf-8'). Encode ('GB18030') deffromserver (self, str): Sys.setdefaultencoding ('GB18030') returnStr.decode ('GB18030') defExecutecmd (self, cmd, iserr=False): cmd=self.toserver (cmd)Print 'exec_command:%s'%cmd stdin, stdout, stderr=Self.ssh.exec_command (cmd) out=Stdout.read () out=Self.fromserver (out) out= Out[:len (out)-1] ifIserr = =True:err=stderr.read () Err=Self.fromserver (ERR) Err= Err[:len (Err)-1] returnOut , errElse: returnOut
Execute command:
print sa.executecmd ('sed-n ' 2,5p '/tmp/test/a')
Output:
Visible, the output line 2nd (the 3rd row of the file) is empty!
Analysis
Break point
To view the full value of the out:
You can see "Selena Gomez:" This line is actually acquired, but you can see that there are spaces and blank lines in front of it, it makes people wonder if there are any special characters in the middle.
I've seen it with the cat command before, but everything looks normal. Think of a problem encountered before: Shell programming Error: "Syntax error near unexpected token '", because of the special character ^m caused.
Use VI to view the contents of a file: Found inside contains a lot of ^@ characters!
^@ is a special character that can be knocked out by Ctrl + V [email protected].
So, the problem is here, because this special character is caused!
Resolves an issue in which Python performs a Linux view File command because the ^@ special symbol causes some content not to appear