我有一个名为file_1.py
it has some code in which, i just have to change a word "file_1" to "file_2"
并保留其他函数的缩进
and save it as file_2.py
单词file_1
有3次出现i have to do this for 100 such times. `file_1.py, file_2.py.....file_100.py`
有什么方法可以实现自动化吗?
答案 0 :(得分:0)
运行此脚本:
import fileinput
with fileinput.FileInput('file_1.py', inplace=True, backup='.bak') as file:
for line in file:
print(line.replace('file_1', 'file_2'), end='')
希望这有帮助:)
答案 1 :(得分:0)
创建一个脚本:
首先:读取文件
new_content = content.replace("file1","file2")
第二名:替换文件名
with open("./file2.py", "w") as f:
f.write(new_content)
第三:写新文件(我建议你写一个新文件)
filenames = ["file" + str(item) for item in range(1,100)]
for filename in filenames:
with open(filename + ".py") as f:
content = f.read()
new_filename = filename[:-1] + str(int(filename[-1]) + 1)
new_content = content.replace(filename,new_filename)
with open("./another_folder" + new_filename + ".py", "w") as f:
f.write(new_content)
如果您有多个文件,请使用
之类的内容String link = "https://stackoverflow.com/questions/ask ";
if (link.split("/").length >= 3 ) {
System.out.println("CORRECT");
}else{
System.out.println("NOT CORRECT");
}