我正在运行一个 python 脚本,该脚本会从文件夹中删除文件。我在虚拟机中运行该脚本...但我收到如下错误:
回溯(最近一次调用最后一次):
文件“C:\Users\Desktop\hyta\python_scripts\python_scr ile_delete.py”,第 66 行,位于
我的代码:
import os
import csv
def extract_sql_filenames_from_yaml(yaml_file):
sql_filenames = {}
with open(yaml_file, 'r') as file:
is_sql_section = False
for line in file:
line = line.strip()
if line == 'sql:':
is_sql_section = True
elif is_sql_section and line.startswith('- name:') and not line.startswith('#'):
filename = line.split(':')[-1].strip()
if filename.endswith('.sql'):
sql_filenames[filename] = sql_filenames.get(filename, 0) + 1
elif line.startswith('-') and not line.startswith('#'):
is_sql_section = False
return sql_filenames
def compare_and_delete_sql_files(folder_a, folder_b, output_csv, sql_filenames):
processed_filenames = set() # To keep track of processed filenames
# Initialize CSV file writer
with open(output_csv, 'w', newline='') as csvfile:
fieldnames = ['SQL File Name', 'Occurrences', 'Match Status', 'Delete Status']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# Iterate through each SQL filename extracted from YAML
for filename, occurrences in sql_filenames.items():
# Skip processing if the filename has already been processed
if filename in processed_filenames:
continue
# Mark the filename as processed
processed_filenames.add(filename)
sql_file_a = os.path.join(folder_a, filename)
match_status = 'Not Matched'
delete_status = 'Not Deleted'
# Check if the file exists in folder_a
if os.path.exists(sql_file_a):
# Compare with folder_b if necessary
if os.path.exists(os.path.join(folder_b, filename)):
with open(sql_file_a, 'r') as f_a, open(os.path.join(folder_b, filename), 'r') as f_b:
lines_a = f_a.readlines()
lines_b = f_b.readlines()
if lines_a == lines_b:
match_status = 'Matched'
delete_status = 'Deleted'
# Delete file from folder_b
os.remove(os.path.join(folder_b, filename))
# Write to CSV
writer.writerow({'SQL File Name': filename, 'Occurrences': occurrences,
'Match Status': match_status, 'Delete Status': delete_status})
# Example usage
folder_a = '/path/to/folder/a'
folder_b = '/path/to/folder/b'
output_csv = 'comparison_results.csv'
yaml_file = 'example.yaml'
sql_filenames = extract_sql_filenames_from_yaml(yaml_file)
compare_and_delete_sql_files(folder_a, folder_b, output_csv, sql_filenames)
PermissionError: [WinError 32] 该进程无法访问该文件,因为该文件正在被另一个进程使用:
错误表明文件已打开,因此无法删除。
发生这种情况是因为您正在删除代码的
open with
块内的文件。
在
os.remove(os.path.join(folder_b, filename))
块之外执行此行 open
。
确保
folder_b
和 filename
变量可用。