'Comparing file name and file size to determine new files
I am writing a script that will sync a remote SFTP folder with a local folder and only download files that do not exist in the remote folder or have a different file size. paramiko has a st_size method that will return the file size. os has a similar method.
What I can not figure out is how to compare the file name and the file size at the same time. I could create a list of lists, like so:
files = [
['file_name1.txt', 123456],
['file_name2.txt', 234567]
]
Then loop over these results.
Or I could create a dictionary where the file name is the key and the file size is the value, then iterate over the dict.
files = {'file_name1.txt': 123456, 'file_name2.txt': 234567}
Is there a best practice here to compare two variables at the same time?
Solution 1:[1]
Using list would be better option, for the following reasons:
- dictionary uses more resources than list. (Only valid for Python < 3.6, this would matter if there are 100s of element)
- List are indexed so, you can access the items without actually knowing both filename or its size, however dictionary needs at least a key or value, which is not possible with dictionary.
Some beneficial links:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Faraaz Kurawle |
