Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

Gray-Ice

个人博客兼个人网站

今天闲着无聊(其实就是不想写web),突然想到之前写博客时分类大小写搞错了找文件的艰辛,就决心写一个能够在当前目录下改变指定文件类型中关键字的脚本。那么先说一下思路吧。

先获取当前目录下的所有文件名称,然后根据文件名来判断是否更改,并且在更改前将将要被更改的文件存进一个文件夹(其实就是备份啦),之后替换关键词即可。

那么下面是代码:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import arrow


class FileChange(object):
"""
file_list will return a list, and "change_keyword.py" won't in it.
change_file will change data.You should have a good think before use it.
do_it will use file_list and change_file to change every file in this directory.
"""

def __init__(self, old_word, new_word, max_range):
"""

:param old_word: Old word.
:param new_word: New word.
:param max_range: the number that you want to instead in every file.
"""
self.old_word = old_word
self.new_word = new_word
self.path = os.path.abspath('') + '\\'
self.max_range = int(max_range)
self.success_list = []

def file_list(self):
"""
This class function will return a list.
:return: It will return a list without "change_keyword.py".
"""
fns = os.listdir()
if os.path.basename(__file__) in fns:
fns.remove(os.path.basename(__file__))
return fns

def back_up(self, filedata, filename):
"""
To back-up the success file.
:param filedata: The data of your file.
:param filename: The name of your file.
:return: Success status.
"""
try:
os.mkdir(self.path + 'back-up')
except FileExistsError:
pass
with open('./back-up/' + filename, 'w') as f:
f.write(filedata)
return filename + 'was back up in directory back-up'

def change_file(self, filename):
"""
:param filename: The file which will be changed.
:return: Return the result, not matter it was success or defeat.
"""
if filename.endswith('.txt') or filename.endswith('.md'):

with open(self.path + filename, 'r') as f:
file_msg = f.read()

if self.old_word in file_msg:
print(self.back_up(filedata=file_msg, filename=filename))
if self.max_range:
file_msg = file_msg.replace(self.old_word, self.new_word, self.max_range)
else:
file_msg = file_msg.replace(self.old_word, self.new_word)

with open(self.path + filename, 'w') as f:
f.write(file_msg)

self.success_list.append(filename)
return 'Success!The file name is :' + filename
else:
return 'Here is no keyword.'
else:
return 'This is not the right type.'

def make_success_file(self):
"""
To make a success list in ./backup/success.txt .
:return: 0
"""
with open('./back-up/success_file_list.txt', 'a') as f:
f.write(arrow.now().format('YYYY-MM-DD HH:mm') + '\n')
f.write(str(self.success_list))
f.write('\n\n')
return 'Made a success list file in ./back-up/success_file_list.txt'

def do_it(self):
"""
In order to change every file in this directory.
"""
for file in self.file_list():
print(self.change_file(file))
if len(self.success_list):
print('This is success list: ', self.success_list)
print(self.make_success_file())
else:
print('Nothing changed.')
return 0


def main():
old_word = input('Please enter your old word')
new_word = input('Please enter your new word')
max_range = input(
'Please enter the number that you want to change in every file.'
'If you enter a string, this program will change every string accord with the old word and the new word \n')
if old_word == '':
print("Old keyword can't be null, please have a good think.")
print('for disabling the old keyword, input it into "old word", and press Enter at "new word"')
input('Press any key and press enter to close this window')
return 0
if max_range.isalnum():
file = FileChange(old_word=old_word, new_word=new_word, max_range=max_range)
file.do_it()
else:
file = FileChange(old_word=old_word, new_word=new_word, max_range=0)
file.do_it()
input('Press enter to close this window.')


if __name__ == '__main__':
main()

代码可以通过git来获取:

1
git clone git@github.com:Gray-Ice/Meaningless-Project..git

你也可以直接访问我的github仓库:Meaningless-Project.
来查看,该仓库时不时会更新一些无用的代码。大概是我有需要的时候,或者有新奇的想法的时候就会更新,不过代码的质量不会太高就是,因为我也不是大佬····并且每个函数我会尽量用英文来写描述,虽然我的英文很菜。。。我会尽量写类,以便调用。若是不懂怎么用直接在我博客下方评论即可,我看到后会第一时间解答。

评论



愿火焰指引你