In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge about what the standard input method of python is, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
You can use the contents of a single file as standard input:
1. Create the file std.pyimport sysfor line in sys.stdin: print (line, end= "")
Run cat / etc/passwd under linux | python std.py or python std.py < / etc/passwd
The contents of / etc/passwd will be printed out and obtained through sys.stdin in the program.
Sys.stdin is a normal file object that has nothing special except to read from standard input. We can also use sys.stdin to call the methods of the file object. Such as calling the read function to read everything in the standard input.
two。 Create the file std2.pyimport sysprint (sys.stdin.read ())
Run cat / etc/passwd | python std2.py
Read the contents of multiple files as standard input
Using fileinput, you can read multiple files given in the command line arguments in turn. In most cases, we simply call the input method of the fileinput module to read the content by line. For example, create a file read_file.py
Import fileinputfor line in fileinput.input (): print (line, end= "")
You can output multiple file contents by running python read_stdin.py / etc/passwd / etc/passwd-, under linux
Because fileinput can read the contents of multiple files, fileinput provides some methods to let us know which file the current reading belongs to.
The common methods in fileinput are:
1. Filename: the name of the file currently being read
2. Fileno: descriptor of the file
3. Filelineno: which line of the current file is the line being read
4. Isfirstline: whether the line being read is the first line of the current file
5. Isstdin: reading the file or reading directly from standard input.
The code is as follows:
Import fileinputfor line in fileinput.input (): meta = [fileinput.filename (), fileinput.fileno (), fileinput.filelineno (), fileinput.isfirstline (), fileinput.isstdin ()] print (meta) print (line, end= ") these are all the contents of the article" what is the standard input method for python ". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.