Use Python to write a script that creates a new folder every day under the system

Source: Internet
Author: User
The function of this program is very simple, is to create a new folder in the system every day. The folder is the current time. This code is seen on the colleague's side, in order to exercise their weak Python ability, so take the time to re-write one. The specific 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 + ' \ \ ' +thisMon Th 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")

When I first started writing, I used the Os.system () to invoke the Windows program, but found that each execution would pop up a python window, which was a hassle. Asked the expert, said the solution is to change the. py file suffix to a. pyw suffix. But it is still not possible to try. Under the guidance of an expert, only to know that the original value needs to change Os.system () to Os.popen () can be.

What is the difference between. Py and. Pyw?

Strictly speaking, there is only one difference between them: when Windows runs them, different execution files are called. Windows runs with Python.exe. Py, run with Pythonw.exe. Pyw. This is purely because the extension. PY is automatically registered as a file running with Python.exe while the. Pyw is registered to run with Pythonw.exe. The "Other differences" between the. Py and. Pyw are all Python.exe And the difference between pythonw.exe.

Compared with Python.exe, Pythonw.exe has the following differences:

    • The console window (also called a DOS window) will not pop up when executed
    • All outputs to the original stdout and stderr are not valid.
    • All reads from the original stdin will only get EOF.

The. Pyw format is a purely graphical interface program designed to run development completion. Users of the Pure graphical interface program do not need to see the console window. When developing a pure graphical interface program, you can temporarily change the. pyw to. py so that the runtime can bring up the console window and see all the error messages.

What is the difference between Os.system () and Os.popen ()?

    • Os.system runs command commands in a child shell and returns the exit status after command execution is complete. This is actually implemented using the C standard library function system (). This function will need to reopen a terminal when executing command commands, and cannot save the execution result of command commands.
    • Os.popen (Command,mode) opens a pipeline between the command process. The return value of this function is a file object that can be read or written (as determined by mode, mode defaults to ' R '). If mode is ' r ', you can use the return value of this function to call read () to get the execution result of the command.
  • 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.