Create and delete a Python file --------- my first Python script

Source: Internet
Author: User

Compared with C ++ and Java, Python is an explanatory language and is suitable for writing scripts. I started learning the Python syntax a long time ago. Today I wrote the first Python script to simplify my daily work. I usually like to create a Word document to take notes, and name it the day. It's like soy sauce: So I have to take a hard look at the number of months today, create a Word document, and then carefully name it like. Although it is not a lot of trouble, you have to think about it. Then I wrote a function that automatically creates the corresponding name WORD document based on the time of the day. In addition, sometimes a new document is created, and nothing is written at the end of the day, so an empty document is placed there. When the empty document accumulates, deleting files one by one is also a little troublesome, so I wrote a function to automatically delete null documents in the directory. Similarly, I added a method to delete Objects Based on the specified minimum file size. The Python code is as follows: copy the code import osdirectory = "E :\\ learning log \" OS. chdir (directory) # change the current working directory cwd = OS. getcwd () # view the current working directory print ("-------------- current working directory:" + cwd + "----------") def deleteBySize (minSize ): "delete files smaller than minSize (unit: K)" files = OS. listdir (OS. getcwd () # list files in the directory for file in files: # print file + ":" + str (OS. path. getsize (file) if OS. path. getsize (file) <minSize * 1000: OS. remove (f Ile) print (file + "deleted. ") return def deleteNullFile (): ''' delete all files with a size of 0 ''' files = OS. listdir (OS. getcwd () # list the files in the directory for file in files: if OS. path. getsize (file) = 0: # Get the file size. If it is a directory, return 0 OS. remove (file) print (file + "deleted") return def create (): ''' create a new file based on the local time, if yes, '''import time # is not created. The specified struct_time (current time by default) is output according to the specified formatted string t = time. strftime ('% Y-% m-% d', time. localtime () suffix = ". docx "newFile = OS. getcwd () + "\" + t + suffix if not OS. path. exists (newFile): f = open (newFile, 'w') f. close () print newFile + "created. "else: print newFile +" already exist. "return hint = ''' funtion: 1 create new file 2 delete null file 3 delete by size q quit \ nplease input number: ''' while True: option = raw_input (hint) if cmp (option, "1") = 0: create () elif cmp (option, "2") = 0: deleteNullFile () elif c Mp (option, "3") = 0: minSize = raw_input ("minSize (K):") deleteBySize (minSize) elif cmp (option, "q ") = 0: print "quit! "Break else: print (" disabled input. please try again... ") Copying code mainly involves file operation functions and time functions. This is my first official Python script. I found that the motivation for solving the problem is the primary productivity! Python is a scripting language with powerful applicability and can be used to solve many practical problems. It is only when you encounter problems that can be solved using Python, can you really improve your abilities?

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.