简体   繁体   English

如何在Python中使用FTPLIB防止覆盖现有文件?

[英]How to Prevent Overwriting a Existing File Using FTPLIB in Python?

I made a python program which stores the user entered data in a sqlite database. 我制作了一个python程序,将用户输入的数据存储在sqlite数据库中。 I want to upload it to ftp server. 我想将其上传到ftp服务器。 I tried using ftplib in python. 我尝试在python中使用ftplib。 Here, database file name is same for all the users who uses the program. 在这里,所有使用该程序的用户的数据库文件名都是相同的。

Here, my problem is 在这里,我的问题是

If user1 upload the file to ftp server, and next user2 uploads it. 如果user1将文件上传到ftp服务器,而下一个user2将其上传。 The file is overwritten. 该文件被覆盖。 How can I stop this? 我该如何阻止呢?

Fairly: How can I stop overwriting a existing file and rename the current(file going to be uploaded) file in ftpserver. 相当:我如何才能停止覆盖现有文件,并在ftpserver中重命名当前文件(要上传的文件)。 So, that I will have those 2 files? 那么,我将拥有这两个文件?

Use ftplib.mlsd() to list the directory before uploading. 在上载之前,请使用ftplib.mlsd()列出目录。 If the given file is already there, then don't upload it. 如果给定的文件已经存在,请不要上传。

Be careful with this -- if two people are uploading at the same time, it's still possible for user A to upload and overwrite user B. 请注意这一点-如果两个人同时上传,则用户A仍然可以上传和覆盖用户B。

Your question lacks details (among the rest: what version of Python you are using, what OS are you on, and most crucially what the naming scheme for the copied files should be). 您的问题缺少详细信息(其余信息包括:正在使用的Python版本,所用的操作系统以及最关键的是所复制文件的命名方案)。

I'll assume that each client program uses a SQLite file named "userdata.db" and that you want to make sure that on the FTP server each copy is identified by the user name. 我假设每个客户端程序都使用一个名为“ userdata.db”的SQLite文件,并且您要确保在FTP服务器上每个副本都由用户名标识。

So User:Pamar will have userdata.db.pamar on ftp server, while User:Lucy will end up with userdata.db.lucy 因此,User:Pamar在ftp服务器上将具有userdata.db.pamar,而User:Lucy将以userdata.db.lucy结尾

(I hope you have some way to be sure that no two users have the same name, btw). (我希望您能通过某种方式确保没有两个用户具有相同的名称,btw)。

The easiest solution I can think of is: 我能想到的最简单的解决方案是:

Use shutil to make a temporary (local) copy of the db with the desidered name, transfer it by FTP, then delete it. 使用shutil来创建具有临时名称的数据库的临时(本地)副本,通过FTP进行传输,然后将其删除。

Ie, in the case of User Pamar you'll have: 即,对于用户Pamar,您将拥有:

  • Step 1: Copy /userhome/pamar/userdata.db -> /userhome/pamar/userdata.db.pamar 步骤1:复制/userhome/pamar/userdata.db-> /userhome/pamar/userdata.db.pamar
  • Step 2: FTP transfer /userhome/pamar/userdata.db.pamar -> FTPServer 第2步:FTP传输/userhome/pamar/userdata.db.pamar-> FTPServer
  • Step 3: Delete /userhome/pamar/userdata.db.pamar 步骤3:删除/userhome/pamar/userdata.db.pamar

It's not very elegant, and you will use some extra space on the (local) filesystem until the copy is completed, and this may be a problem if the userdata.db is particularly large (but then you would probably not use sqlite in the first place). 它不是很优雅,在复制完成之前,您将在(本地)文件系统上使用一些额外的空间,如果userdata.db特别大,则可能会出现问题(但是您可能首先不会使用sqlite地点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM