使用Python从sftp文件夹中删除文件时出现问题

问题描述 投票:0回答:1

我正在尝试将文件移动到与存储文件的文件夹位于同一目录级别的备份文件夹中。将文件下载到本地目录并将文件上传回备份文件夹可以正常工作,但无法从现有远程文件夹中删除文件。我希望在处理每个文件后逐个删除文件,而不是将路径存储在列表中并立即删除所有文件。我不知道出了什么问题。希望寻求建议。

读取文件 test_abs_2022135201552.pslog 时出错:错误消息

下面是代码片段。我已经测试了代码的“放置”和“获取”部分是否有效。

sftp.remove(file_path)
行是出现问题的行。

source_path = "/project/"
local_path = r'C:\Users\Desktop\project'

 df_staging = pd.DataFrame()
    
def main():


    transport = paramiko.Transport((sftp_host, sftp_port))
    transport.connect(username=sftp_username, password=sftp_password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    global df_staging

    try:

        main_folders = main_folders = sftp.listdir(source_path) 

        for main_folder in main_folders:
            main_folder = os.path.join(source_path, main_folder) 
            station_folders = sftp.listdir(main_folder_path) # List the station folder names 

            # To create a backup folder if it does not exist in remote server
            if 'backup' not in sftp.listdir(main_folder_path):
                sftp.mkdir(os.path.join(main_folder_path, 'backup'))
                print("Backup folder created")
                
            # To create family folder if it does not exist in local dir
            if main_folder not in os.listdir(local_path):
                os.mkdir(os.path.join(local_path, main_folder))

            for station_folder in station_folders:
                if station_folder != 'backup':
                    station_folder_path = os.path.join(main_folder_path, station_folder) 
                    remote_files = sftp.listdir(station_folder_path) 


                   local_file_path = os.path.join(local_path, main_folder, file)
                   remote_backup_path = os.path.join(main_folder_path, 'backup', file)
                   # Transfer file between remote to local
                   sftp.get(file_path, local_file_path)
                   # Transfer file back from local to remote
                   sftp.put(local_file_path, remote_backup_path)
                   # Remove file from remote station folder (This line is giving me the errors!!!)
                   sftp.remove(file_path)
                                            
                   print(f"Done transfering: {file} ")
                   print("\n")
                                        
                   except Exception as e:
                      print(f"Error reading file {file}: {str(e)}")


                    lst_files_to_delete = [path for path in lst_all_files if path not in lst_files_to_transfer]
                    print("Files to delete:", lst_files_to_delete)

                    for delete_file_path in lst_files_to_delete:
                        print(f"Deleting file: {delete_file_path}")
                        sftp.remove(delete_file_path)
                        print(f"File deleted successfully")
python sftp delete-file file-transfer pysftp
1个回答
0
投票

由于我的文件仍然在主代码中打开读取,因此程序无法删除该文件。通过将

remove
行移出正在读取文件的循环来解决该问题

© www.soinside.com 2019 - 2024. All rights reserved.