写了一个简单的删除文件夹内的TXT文档25行后的脚本,但是双击后一闪而过,是脚本出问题了吗?
代码如下:
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> def delete_lines(filename, start_line):
... with open(filename, 'r') as file:
... lines = file.readlines()
...
... if len(lines) < start_line:
... print(f"文件 {filename} 行数不足 {start_line} 行,无法删除。")
... return
...
... with open(filename, 'w') as file:
... file.writelines(lines[:start_line - 1])
...
... print(f"文件 {filename} 删除了第 {start_line} 行至最后一行。")
... def batch_delete_lines(folder_path, start_line):
... for filename in os.listdir(folder_path):
... if filename.endswith(".txt"):
... full_path = os.path.join(folder_path, filename)
... delete_lines(full_path, start_line)
... if __name__ == "__main__":
... folder_path = r"C:\Users\Administrator\Desktop\pyhon"
... start_line = 25
...
... batch_delete_lines(folder_path, start_line)
代码如下:
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> def delete_lines(filename, start_line):
... with open(filename, 'r') as file:
... lines = file.readlines()
...
... if len(lines) < start_line:
... print(f"文件 {filename} 行数不足 {start_line} 行,无法删除。")
... return
...
... with open(filename, 'w') as file:
... file.writelines(lines[:start_line - 1])
...
... print(f"文件 {filename} 删除了第 {start_line} 行至最后一行。")
... def batch_delete_lines(folder_path, start_line):
... for filename in os.listdir(folder_path):
... if filename.endswith(".txt"):
... full_path = os.path.join(folder_path, filename)
... delete_lines(full_path, start_line)
... if __name__ == "__main__":
... folder_path = r"C:\Users\Administrator\Desktop\pyhon"
... start_line = 25
...
... batch_delete_lines(folder_path, start_line)