How to remove comments from Python files

Source: Internet
Author: User

How to remove comments from Python files

This article describes how to remove comments from Python files. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

#! /Usr/bin/python

#-*-Coding: GBK -*-

# Writer: xmnathan

# Annotate the py file

Import re

Import OS

Import ConfigParser

Python = 'note'

Def ReadIni (path, section, option): # file path, chapter, keyword

# Read ini

Cf = ConfigParser. ConfigParser ()

Cf. read (path)

Value = cf. get (section, option) # If getint () is used, the data type is directly read as an integer.

Return value

Def IsPassLine (strLine ):

# Whether the row can be ignored

# Ignore the regular expression list of rows

RegularExpressions = ["""/'.*#.*/'""","""/".*#.*/"""",

"""/'/'/'. *#. */'/'/'""","""/"/"/". *#. */"]

For One in RegularExpressions:

Zz = re. compile (One)

If re. search (zz, strLine) = None:

Continue

Else:

Return True # ignore if a match exists.

Return False

Def ReadFile (FileName ):

# Reading and processing files

Fobj = open (FileName, 'R ')

AllLines = fobj. readlines ()

Fobj. close ()

NewStr =''

LogStr = '/n % 20 s/N' % (FileName. split (' // ') [-1]) # output log

Nline = 0

For eachiline in AllLines:

Index = eachline. find ('#') # obtain the location index with the comment clause '#'

If index =-1 or nline <3 or IsPassLine (eachline ):

If eachiline. strip ()! = '': # Exclude empty rows

NewStr = NewStr + eachiline

Else:

If index! = 0:

NewStr = NewStr + eachiline [: index] + '/N' # extract the comments

LogStr + = "ChangeLine: % s/t % s" % (nline, eachline [index:])

Nline + = 1

Return NewStr, LogStr

Def MakeCleanFile (SrcPath, DescPath, FileList ):

FLog = open (DescPath + '// 'employee 'cleannotelog.txt', 'w ')

For File in FileList:

CurStr, LogStr = ReadFile (SrcPath + '//' + File)

FNew = open (DescPath + '//' + File, 'w ')

FNew = write (curStr)

FNew. close ()

FLog. write (LogStr)

FLog. close ()

Def Main ():

# Obtain the path of the source folder and target folder from ini

IniPath = OS. getcwd () + '//' + PtName + '. ini'

SrcPath = ReadIni (IniPath, PyName, 'srcpath ') # source folder

DescPath = ReadIni (IniPath, PyName, 'descpath') # destination folder

# If the destination folder does not exist, create it

If not OS. path. exists (DescPath ):

OS. makedirs (DescPath)

FileList = []

For files in OS. walk (SrcPath ):

For FileName in files [2]:

If FileName. split ('.') [-1] = 'py ':

FileList. append (FileName)

MakeCleanFile (SrcPath, DescPath, FileList)

If _ name __= = '_ main __':

Main ()

Print '>>> End <'

OS. system ('pause ')

Ps: configuration file CleanNote. ini format

?

1

2

3

[CleanNote]

SrcPath = E:/test

DescPath = E:/test/newfiles

Batch remove comments of the py file in the specified source folder, and generate a copy and a specified destination folder

I hope this article will help you with Python programming.

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.