Working with files¶
Assuming you have connected to your site.
Getting info about a file¶
To get information about a file:
>>> file = site.images['Example.jpg']
where file
is now an instance of Image
that you can query for various properties:
>>> file.imageinfo
{'comment': 'Reverted to version as of 17:58, 12 March 2010',
'descriptionshorturl': 'https://commons.wikimedia.org/w/index.php?curid=6428847',
'descriptionurl': 'https://commons.wikimedia.org/wiki/File:Example.jpg',
'height': 178,
'metadata': [{'name': 'MEDIAWIKI_EXIF_VERSION', 'value': 1}],
'sha1': 'd01b79a6781c72ac9bfff93e5e2cfbeef4efc840',
'size': 9022,
'timestamp': '2010-03-14T17:20:20Z',
'url': 'https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg',
'user': 'SomeUser',
'width': 172}
You also have easy access to file usage:
>>> for page in image.imageusage():
>>> print('Page:', page.name, '; namespace:', page.namespace)
See the API reference
for more options.
Caution
Note that Image.exists
refers to whether a file exists locally. If a file
does not exist locally, but in a shared repo like Wikimedia Commons, it will
return False
.
To check if a file exists locally or in a shared repo, you could test if
image.imageinfo != {}
.
Uploading a file¶
>>> site.upload(open('file.jpg'), 'destination.jpg', 'Image description')