Demand Analysis
1, read all the files in the specified directory
2, read the specified file, the output file content
3. Create a file and save it to the specified directory
Realization process
Python write code concise and efficient, to achieve the above features only about 40 lines of code ~ Yesterday with Java to write a write, create, copy, rename files to nearly 60 lines of code;
The price of simplicity, though, is sacrificing a little bit of runtime speed, but as hardware performance improves, the difference in runtime speed will get smaller and smaller until it's unnoticeable to humans~
#-*- coding: UTF-8 -*- ''' 1, read all the files in the specified directory 2, read the specified file, output file content 3, create a file and save it to the specified directory ''' import os # Iterate through the specified directory and display all file names in the directory def eachFile(filepath): pathDir = (filepath) for allDir in pathDir: child = ('%s%s' % (filepath, allDir)) print ('gbk') # .decode('gbk') is the solution to the problem of Chinese displaying garbled codes # Read the contents of the file and print it def readFile(filename): fopen = open(filename, 'r') # r is for read for eachLine in fopen: print "The following was read:",eachLine () # Input multiple lines of text, write to a specified file and save to a specified folder def writeFile(filename): fopen = open(filename, 'w') print "\r Please enter as many lines of text as you like."," ( importation .No. Enter to save)" while True: aLine = raw_input() if aLine != ".": ('%s%s' % (aLine, )) else: print "File saved!" break () if __name__ == '__main__': filePath = "D:\\FileDemo\\Java\\" filePathI = "D:\\FileDemo\\Python\\" filePathC = "C:\\" eachFile(filePathC) readFile(filePath) writeFile(filePathI)
nonchalant workmanship is the best thing that can happen (idiom); study diligently
Recently I tried several common Python IDEs and found that Subline tx2 has bad support for Chinese, and NotePad++ is not convenient to customize the color of the code.
Used to use or Eclipse is the smoothest, after installing the PyDev plug-in, write Python code is very convenient;
Author: Jiang Zhi Yi