This article mainly introduces how to use Python to compile a script to create a folder in the system every day. Although this implementation sounds boring... however, it is a small practice of learning the OS and time modules. If you need a friend, you can refer to the simple function of this program, that is, to create a folder in the system every day. Folder is the current time. This code was seen by my colleagues. In order to train my weak Python capabilities, I took the time to write a new one. The Code is as follows:
import time,os basePath = 'F:\\work\\' thisYear = str(time.localtime()[0]) thisMonth = str(time.localtime()[1]) thisDay = time.strftime("%Y-%m-%d", time.localtime()) yearPath = basePath + thisYear monthPath = basePath + thisYear + '\\' +thisMonth dayPath = basePath + thisYear + '\\' +thisMonth + '\\' + thisDay if not os.path.exists(yearPath): os.mkdir(yearPath) if not os.path.exists(monthPath): os.mkdir(monthPath) if not os.path.exists(dayPath): os.mkdir(dayPath) os.popen("explorer.exe" + " " + dayPath) os.popen("exit")
At the beginning, I used OS. system () to call the windows program, but I found that a python window will pop up every execution, which is very troublesome. I asked someone else, saying that the solution is to change the. py file suffix TO THE. pyw suffix. But I still cannot try it. Under the guidance of a senior citizen, the original value must be changed to OS. popen.
What is the difference between. py and. pyw?
Strictly speaking, there is only one difference between them: different execution files are called when Windows run them. Use python.exe to run. py, and use pythonw.exe to run. pyw. This is because the extension. py is automatically registered as a file running with python.exe when Python is installed, and. pyw is registered as running with pythonw.exe .. The difference between py and. pyw is the difference between python.exe and pythonw.exe.
Compared with python.exe, pythonw.exe has the following differences:
- The console window (also called DOS window) is not displayed during execution)
- All outputs to the original stdout and stderr are invalid.
- All reads from the original stdin will only get the EOF
The. pyw format is designed to run the Developed Graphic Interface Program. Users of graphic interfaces do not need to see the console window. When developing a graphic interface program, you can temporarily change. pyw to. py, so that you can call up the console window during running and see all error messages.
What is the difference between OS. system () and OS. popen?
- OS. system (command) runs the command in a sub-shell and returns the exit status after the command is executed. This is actually implemented using the C standard library function system. This function needs to re-open a terminal when executing the command, and cannot save the command execution result.
- OS. popen (command, mode) opens a pipeline between the command process and the command process. The return value of this function is a file object that can be read or written (determined by the mode, the default mode is 'R '). If the mode is 'R', you can use the return value of this function to call read () to obtain the execution result of the command.