gpt4 book ai didi

Problems with deleting file from sftp folder using Python(使用Python从sftp文件夹中删除文件的问题)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:21 26 4
gpt4 key购买 nike



I am trying to move files into a backup folder which is on the same directory level as the folder where the files are stored. The downloading of the file into a local directory and uploading the file back to the backup folder works but the files can't be deleted from the existing remote folder. I wish to remove the files one by one after each file has been processed instead of storing the paths in a list and removing all at once. I have no idea what is wrong. Hope to seek advice.

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



Error reading file test_abs_2022135201552.pslog: Bad message



Below is a snippet of the code. I have tested that the 'put' and 'get' part of the code works. The line sftp.remove(file_path) is the one giving problems.

下面是一段代码。我已经测试了代码的“put”和“get”部分是否有效。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")

更多回答

Which line fails?

哪条线路出现故障?

This line: # Remove file from remote station folder sftp.remove(file_path)

此行:#从远程工作站文件夹sftp.Remove(file_path)中删除文件

No idea. My guess is the code was written for another server or you need additional configuration. Does the documentation say anything? Can you check the library code?

不知道。我的猜测是,代码是为另一台服务器编写的,或者您需要额外的配置。文件上有说明吗?你能查一下图书馆的代码吗?

Do you need the for loop to reproduce the problem? Do you need the list_dir, mkdir or put? If not, reduce your code to minimal reproducible example. We are not interested in your whole program. I assume just the get is useful here, as it shows that the file_path is valid. + Can you delete the file using sftp? If you can, show us equivalent transcript of the sftp session, with the same paths as your code is using.

优秀答案推荐

As my file was still opened to be read in the main code, the program was unable to delete the file. The problem is resolved by moving the remove line out of a loop that is reading the file

由于我的文件仍在主代码中打开以供读取,程序无法删除该文件。通过将remove行移出正在读取文件的循环,可以解决该问题



Essentially, sftp.remove can only work when the file is closed. In the previous example where I encountered the problem, sftp.remove was placed inside with sftp.open. After moving it out, it works. Below is the portion of the main code that I was referring to:

本质上,sftp.remove只能在文件关闭时工作。在我遇到这个问题的上一个例子中,sftp.remove与sftp.open一起放在里面。把它移出去后,它就工作了。以下是我所指的主要代码部分:


with sftp.open(file_path, 'r') as remote_file:
# Main Code


### After file is closed, perform file upload and removal only after uploaded to DB ###
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 (***Only works when file is not open!)
sftp.remove(file_path)

更多回答

Always close your file handles after you're done using them to avoid such problems in the future.

在使用完文件句柄后,请始终关闭它们,以避免将来出现此类问题。

What "main code"?

什么是“主代码”?

I have posted an answer below

我在下面发布了一个答案

Well, there's no sftp.open in your original code. You have failed to post minimal reproducible example.

嗯,在您的原始代码中没有sftp.open。您未能发布可重复的最小示例。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com