我是编程新手,有一个问题:
我尝试编辑一些 .vtt 文件,我想从文本中删除某些子字符串。该文件应保持其结构。为此,我复制了文件夹中的 .vtt 文件并将其更改为 .txt 结尾。现在我运行这个简单的代码:
$stats bob#0001
我收到此错误消息:
import os
file_index = 0
all_text = []
path = "/Users/username/Documents/programming/IMS/Translate/files/"
new_path = "/Users/username/Documents/programming/IMS/Translate/new_files/"
for filename in os.listdir(path):
if os.path.isfile(filename): #check if there is a file in the directory
with open(os.path.join(path, filename), 'r') as file: # open in read-only mode
for line in file.read().split("\n"): #read lines and split
line = " ".join(line.split())
start_index = line.find("[") #find the first character of string to remove, this returns the index number
last_index = start_index + 11 #define the last index to be removed
if start_index != -1:
line = line[:start_index] + line[last_index:] #The new line to slice the first charaters until the one to be removed, and add the others that need to stay
all_text.append(line)
else:
line = line[:]
all_text.append(line)'''
我搜索了不同的论坛,更改为 encoding="utf16",但无济于事。奇怪的是,它确实更早地起作用了。然后我写了一个程序来自动重命名我的文件,之后,它抛出了这个错误。我已经清除了文件夹中的所有文件,再次复制了原始文件......无法让它工作。非常感谢您的帮助,因为我真的不知道去哪里找。谢谢