In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the input and output cases of the process in the python language. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Input in the process is usually saved in variables (that is, memory) first, and this input can come from keyboards (also known as standard input), files, socket, serial ports, and so on.
Similarly, data processed by the process can also be output from variables (that is, memory) to the screen (also known as standard output), files, socket, serial ports, and so on.
Small programs here only introduce input from the keyboard or file, and output to the screen or file. For socket or serial port, find another time to introduce it.
(1) enter from the keyboard
This is a common program interaction and can be achieved using raw_input or input functions.
The raw_input reads the keyboard input intact, and the input function attempts to operate on the input before returning it.
The input function can enter numbers, arithmetic expressions, functions, or statements that return data structures, and the input function processes them before returning the content.
The input function cannot enter a string that cannot be processed.
Examples are as follows:
(2) Import from file
That is, to read the contents of the file.
The following is an example of frequently encountered file operations, reading the contents of the file and printing it out:
When you open a file using the open function, the first parameter is the file name and the second parameter is the access mode.
The default access mode is read-only, which is'r'.
Access modes are likely to be used frequently. Here is a screenshot of the meaning of all access modes. The image is from http://www.runoob.com/python/python-files-io.html:
(3) output to screen
Output to the screen, also known as standard output, uses the function print.
Almost all of the python examples involve the use of print, which is no longer a separate example here.
(4) Export to file
To output to a file is to write a file.
In the open file, use the access mode with write, and then use the write function to write the file. The example is as follows:
Above, introduced the data input and output in the python process, including the operation of the file content, including reading and writing.
In practical applications, in addition to file reading and writing, file renaming, deletion, and folder operations (such as traversal, creation, deletion, etc.) are also often encountered, so here to do some more introduction.
(a) renaming and deletion of documents
Using the os module, import this module:
Import os
Rename the file, using the os.rename function, such as:
To delete files, use the os.remove function, such as:
(B) folder traversal
There are generally two ways to traverse the folder to find all the files, one is to use the os.listdir function, and the other is to use the os.walk function, which is demonstrated here.
The directory structure of Xiao Cheng is as follows:
Using os.listdir, write a recursive lookup function with the following code and effect:
The above traversal functions use reused algorithm routines. For this classic algorithm routine, readers can follow the "Guangzhou Xiaocheng" Wechat official account and refer to the corresponding article in the "algorithm" menu item.
Another way is to use the os.walk function. The code using this function can be very concise, and the code and effect are as follows:
(C) folder creation and deletion
Folder creation, using the os.mkdir function.
Delete the folder, using the os.rmdir or shutil.rmtree function.
The sample code and effect are as follows:
It should be noted that os.rmdir can only be used to delete empty directories, which will result in an exception if the directory is not empty. For empty or non-empty directories, you can use the shutil.rmtree function to delete them, such as:
Shutil.rmtree ('2')
In addition, deleting a directory that does not exist will cause an exception, and creating an existing directory will also cause an exception, so you can determine whether the directory exists before the operation, such as:
Import os, shutil
If os.path.exists ('2'):
Shutil.rmtree ('2')
If not os.path.exists ('3'):
Os.mkdir ('3')
This is the end of this article on "input and output cases of processes in python language". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.