Resolves an issue in which Python performs a Linux view File command because the ^@ special symbol causes some content not to appear

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.