如何使用 ascii 比较 Windows 和 Linux 上的文件大小?
我正在编写一个 Python 脚本,它将把一堆文件上传到 FTP 站点。为了检查文件是否已更改,我正在比较文件大小。问题是,我上传的文件有 \r\n 行结尾,但通过 FTP(ascii 模式到 Linux 机器)传输会转换为 \n 行结尾。显然,我在此过程中丢失了一堆字节,因此我无法再比较文件大小。
我不确定继续这里的最佳方法。检查文件大小时即时从 \r\n 转换为 \n 吗?以二进制模式上传所有内容?停止比较文件大小?
I'm working on a Python script that will upload a bunch of files to an FTP site. To check to see whether the file has changed, I'm comparing file sizes. The problem is, the files I'm uploading have \r\n line endings, but transferring via FTP (ascii mode to a Linux box) converts to \n line endings. Obviously I'm losing a bunch of bytes in this process, so I can't compare file sizes any more.
I'm not sure the best way to proceed here. Convert from \r\n to \n on the fly when checking file sizes? Upload everything in binary mode? Stop comparing file size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会根据文件大小检查文件是否已更改。由于它是 ascii 文本,因此文件可能已更改但仍然具有完全相同的字节数。
I would not base your check on whether the file has changed based on filesize. Since it is ascii text, the file could have changed and still have the exact same number of bytes.
使用文件大小是一个坏主意,除非它们只有在发生变化时才会增长(但通常情况并非如此,除非它们是日志文件或其他文件)。
一种选择是跟踪每个文件(也可以上传到 ftp 服务器)的校验和(通常使用 md5sum)。如果校验和与主校验和文件中的内容匹配,则没有任何更改,否则上传更改的文件并更新该文件的 md5sum。
Using file sizes is a bad idea unless they can only grow if they change (typically not the case, though, unless they are log files or something).
One option is to keep track of a checksum (md5sum is typically what is used) for each file (which could be uploaded to the ftp server as well). If the checksum matches what is in the master checksum file, then nothing has changed, otherwise upload the changed file and update the md5sum of that file.