Delete the spaces left by the blog garden when copying Python code

Source: Internet
Author: User

I haven't posted a blog in the blog Center for four months. I found that I have another function: CopyCode. This is a good thing, but unfortunately, I copied the Python code, and the blog garden helped me add a space at the first byte of each line, the strong indentation style of Python makes the copied code unable to run. Removing spaces is the only method. It is feasible to open the code file and manually delete the first space in each line, but it is unrealistic. This article is born for this, and the second is =, =.

Let's start with a simple code, which is mainly the code file that the user enters to delete spaces,ProgramCreate a new backup. py file in the running directory, open the original file, remove spaces one by one, and write it into the new file. The code is relatively simple:

Version_1

 1 Ori_file = raw_input ( '  Input:  ' )
2 New_file = open ( ' Backup. py ' , ' W ' )
3 For Line In Open (ori_file ):
4 If Line:
5 Line = line [1:]
6 New_file.write (line)
7 New_file.close ()
8 Raw_input (' Done. Press any key to continue... ' )

 

The goal has been achieved, but the trainer may wish to set more requirements for one of the purposes. Below are my ideas, including general design ideas.

1. print Selection: enter a directory to process all files under the directory. If the number of input files is not included, the file will be regarded as the current directory to process the files found, return nonexistent files to the user.

2. judgment input: if it is a directory, all the files under the directory will be placed in the list, and you can traverse the list of files; if it is a file, the input multiple file names can be like this processor for file in input. split ().

In addition, if I want to accept mixed input of directories and files, I can put all the input files in the list, judge the elements one by one as directories or files, and then process them accordingly. The Code is as follows:

Version_2

 1   Import OS
2 Import OS. path as op
3
4 _ All __ = [ ' Deal_dir ' , ' Deal_file ' ]
5
6 Def Begin_bak ():
7 Deal_with = raw_input ( ' Input: ' )
8 For Each In Deal_with.split ():
9 If Op. isdir (each ):
10 Deal_dir (each)
11 Elif Op. isfile (each ):
12 Deal_file (each)
13 Else :
14 Print " Cannot find % S. " % Each
15
16 Def Deal_dir (each ):
17 Dir_file = []
18 For Root, dirs, fnames In OS. Walk (each ):
19 If Fnames:
20 For FN In Fnames:
21 Dir_file.append (op. Join (root, FN ))
22 For Each In Dir_file:
23 Baking (each)
24
25 Def Deal_file (each ):
26 If Op. exists (each ):
27 Each = op. Join ( ' . ' , Each)
28 Else :
29 Print ' Cannot find % S. ' % Each
30 Baking (each)
31
32 Def Baking (each ):
33 New_file = open (op. splitext (each) [0] + ' . Bak. py ' , ' W ' )
34 File_list = List (open (each ))
35 For Line In File_list:
36 If Line = file_list [0]:
37 Line = line
38 Else :
39 Line = line [1:]
40 New_file.write (line)
41 New_file.close ()
42
43 If _ Name __ = ' _ Main __ ' :
44 Begin_bak ()
45 Raw_input ( ' Done. Press any key to continue... ' )

After running, you can see input: Enter the Directory and file to be processed, and "." indicates the current directory. If the file name does not contain a path, the current path is used by default.

 

Now the full text is complete. Of course, I hope the blog Park will fix this bug as soon as possible. Or is it intentional?

Done. Press any key to continue...

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.