更改子目录中特定文件类型的行
Changing lines in specific file types in subdirectories
我正在尝试创建一个脚本来搜索用户输入文件路径并打开所有 .cix 文件类型并替换第 27 行。
我目前所做的工作是在单个文件夹中一次打开一个文件并修改所需的行。但是,如果我尝试在包含子文件夹的文件夹上使用它,它就无法正常工作。它正在打开并保存文件,但没有更改行,我不知道哪里出错了。
from tkinter import *
import os
root = Tk()
root.geometry('400x75')
root.title('Labels Off')
dirLabel = Label(root, text='Directory Path:').pack()
e = Entry(root, width='50')
e.pack()
os.listdir(path='.')
def replace(filename):
f = open(filename, 'r')
lines = f.readlines()
print(filename)
f.close()
if 'ENABLELABEL=1' in lines[27]:
lines[27] = ' ENABLELABEL=0\n'
elif 'ENABLELABEL=0' in lines[27]:
lines[27] = ' ENABLELABEL=1\n'
else:
print('err')
f = open(filename, 'w')
f.write(''.join(lines))
f.close()
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
counter = 0
for file in allFiles:
if file.endswith('.cix'):
replace(file)
else:
allFiles.pop(counter)
counter += 1
return allFiles
def run():
dirName = e.get()
getListOfFiles(dirName)
e.delete(0, END)
submit = Button(root, text='Run', command=run).pack()
mainloop()
getListOfFiles
returns 当前目录和所有子目录的文件列表 并且 在每个 .cix
上调用 replace
] 此列表中的文件。
但它以递归方式执行此操作,因此对于更深一层子目录中的每个 .cix
文件,replace
已被调用。
因为 replace
基本上是在 ENABLELABEL=1
和 ENABLELABLE=0
之间切换一行,对于 子目录中的每个 .cix
文件奇数根目录下的层数,实际上没有变化。
解决方案是先得到一个完整的文件列表,然后然后为每个[=13]调用replace
=] 文件:
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
# no replace(file) here
return allFiles
def run():
dirName = e.get()
allFiles = getListOfFiles(dirName)
# instead, do it here
for file in allFiles:
if file.endswith('.cix'):
replace(file)
e.delete(0, END)
(我也不明白你想用 counter
实现什么。我只是省略了它。)
好的,我不知道我以前创建文件列表的方法到底出了什么问题,但有些地方不对劲。尝试了一段时间后,我用这个替换了我以前的方法,现在它完美地工作了,
def getListOfFiles(dirName):
allFiles = []
for r, d, f, in os.walk(dirName):
for file in f:
if '.cix' in file:
allFiles.append(os.path.join(r, file))
for file in allFiles:
replace(file)
return allFiles
我正在尝试创建一个脚本来搜索用户输入文件路径并打开所有 .cix 文件类型并替换第 27 行。
我目前所做的工作是在单个文件夹中一次打开一个文件并修改所需的行。但是,如果我尝试在包含子文件夹的文件夹上使用它,它就无法正常工作。它正在打开并保存文件,但没有更改行,我不知道哪里出错了。
from tkinter import *
import os
root = Tk()
root.geometry('400x75')
root.title('Labels Off')
dirLabel = Label(root, text='Directory Path:').pack()
e = Entry(root, width='50')
e.pack()
os.listdir(path='.')
def replace(filename):
f = open(filename, 'r')
lines = f.readlines()
print(filename)
f.close()
if 'ENABLELABEL=1' in lines[27]:
lines[27] = ' ENABLELABEL=0\n'
elif 'ENABLELABEL=0' in lines[27]:
lines[27] = ' ENABLELABEL=1\n'
else:
print('err')
f = open(filename, 'w')
f.write(''.join(lines))
f.close()
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
counter = 0
for file in allFiles:
if file.endswith('.cix'):
replace(file)
else:
allFiles.pop(counter)
counter += 1
return allFiles
def run():
dirName = e.get()
getListOfFiles(dirName)
e.delete(0, END)
submit = Button(root, text='Run', command=run).pack()
mainloop()
getListOfFiles
returns 当前目录和所有子目录的文件列表 并且 在每个 .cix
上调用 replace
] 此列表中的文件。
但它以递归方式执行此操作,因此对于更深一层子目录中的每个 .cix
文件,replace
已被调用。
因为 replace
基本上是在 ENABLELABEL=1
和 ENABLELABLE=0
之间切换一行,对于 子目录中的每个 .cix
文件奇数根目录下的层数,实际上没有变化。
解决方案是先得到一个完整的文件列表,然后然后为每个[=13]调用replace
=] 文件:
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
# no replace(file) here
return allFiles
def run():
dirName = e.get()
allFiles = getListOfFiles(dirName)
# instead, do it here
for file in allFiles:
if file.endswith('.cix'):
replace(file)
e.delete(0, END)
(我也不明白你想用 counter
实现什么。我只是省略了它。)
好的,我不知道我以前创建文件列表的方法到底出了什么问题,但有些地方不对劲。尝试了一段时间后,我用这个替换了我以前的方法,现在它完美地工作了,
def getListOfFiles(dirName):
allFiles = []
for r, d, f, in os.walk(dirName):
for file in f:
if '.cix' in file:
allFiles.append(os.path.join(r, file))
for file in allFiles:
replace(file)
return allFiles