linux_access
linux_access Documentation
A module to access Mobile Devices from UNIX via USB connection. After connecting to gnome desktop the devices can be found under: /run/user/1000/gvfs/ in the normal file system. If on an other desktop environment, for example KDE, then any process that uses libmtp will be killed and this module will access libmtp.
Author: Heribert Füchtenhans
Version: 2025.6.26
For examples please look into the examples directory.
All functions through IOError when a communication fails.
Requirements
- OS
- Linux (GNOME and KDE)
The module contains the following functions:
- 'get_portable_devices' Get a list (instances of PortableDevice) of all connected portable devices.
- 'get_content_from_device_path' - Get the content (files, dirs) of a path as instances of PortableDeviceContent
- 'walk' - Iterates ower all files in a tree.
- 'makedirs' - Creates the directories on the MTP device if they don't exist.
The module contains the following classes:
- 'PortableDevice' - Class for one portable device found connected
- 'PortableDeviceContent' - Class for one file, directory or storage
Information to the filenames used in MTP:
- The filename consists of the following part: devicename/storagename/foldername/....
- The devicename can be found in PortableDevice.devicename
- The storagename is in content returned by PortableDevice.get_content It's the the name attribute
- Every PortableDeviceContent has an attribute 'full_filename' that contains the whole path of that content
Examples:
>>> import mtp.linux_access
>>> devs = mtp.linux_access.get_portable_devices()
>>> len(devs) >= 1
True
>>> str(devs[0])[:16]
'PortableDevice: '
>>> devs[0].close()
PortableDevice
Class with the infos for a connected portable device. This instances of this class are created intern, please only use the methods and attributes
Methods:
Name | Description |
---|---|
close |
Must be called when the device is no more needed. This frees the resources |
get_content |
Get the content (Storages) of the device |
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the MTP device |
description |
str
|
Description for the device |
serialnumber |
str
|
Serialnumber of the device |
devicename |
str
|
The full name of the device. Use it when building pathes |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Source code in mtp/linux_access.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
__init__(device)
Init the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str | _Pointer[LIBMTP_RawDevice]
|
Linux path to the device on Gnome or a libmtp device pointer on KDE |
required |
Source code in mtp/linux_access.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
close()
Close the connection to the device. This must be called when the device is no more needed.
Source code in mtp/linux_access.py
182 183 184 185 |
|
get_content()
Get the content of a device, the storages
Returns:
Type | Description |
---|---|
list[PortableDeviceContent]
|
A list of instances of PortableDeviceContent, one for each storage |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()
>>> len(stor)
2
>>> str(stor[0])[:31]
'PortableDeviceContent Interner '
>>> dev[0].close()
Source code in mtp/linux_access.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
PortableDeviceContent
Class for one file, directory or storage with it's properties. This instances of this class are created intern, please only use the methods and attributes
Methods:
Name | Description |
---|---|
get_children |
Get the children of this content (files and directories) |
get_child |
Returns a PortableDeviceContent for one child whos name is known. |
get_path |
Returns a PortableDeviceContent for a child who's path in the tree is known. |
create_content |
Creates an empty directory content in this content. |
upload_file |
Upload of a file to MTP device. |
download_file |
Download a file from MTP device. |
remove |
Deletes the current directory or file. |
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Directory-/Filename of this content |
fullname |
str
|
The full path name |
date_modified |
datetime
|
The file modification date |
size |
int
|
The size of the file in bytes |
content_type |
int
|
Type of the entry. One of the WPD_CONTENT_TYPE_ constants:
|
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Source code in mtp/linux_access.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
__init__(port_device, dirpath, storage_id, entry_id, typ, size=0, date_modified=0)
Instance constructor
Source code in mtp/linux_access.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
__repr__()
Source code in mtp/linux_access.py
473 474 475 476 |
|
create_content(dirname)
Creates an empty directory content in this content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dirname
|
str
|
Name of the directory that shall be created |
required |
Returns:
Type | Description |
---|---|
PortableDeviceContent
|
PortableDeviceContent for the new directory |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()
>>> mycont = stor[0].get_path(f"{stor[0].full_filename}/MyMusic")
>>> if mycont: _ = mycont.remove()
>>> cont = stor[0].create_content("MyMusic")
>>> str(cont)
'PortableDeviceContent MyMusic (1)'
>>> dev[0].close()
Source code in mtp/linux_access.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
download_file(outputfilename)
Download of a file from MTP device The used ProtableDeviceContent instance must be a file!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outputfilename
|
str
|
Name of the file the MTP file shall be written to. Any existing content will be replaced. |
required |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()
>>> cont = stor[0].get_path(f"{stor[0].full_filename}/DCIM/Camera/IMG_20241210_160830.jpg")
>>> name = 'picture.jpg'
>>> cont.download_file(name)
>>> os.remove(name)
>>> dev[0].close()
Source code in mtp/linux_access.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
get_child(name)
Returns a PortableDeviceContent for one child whos name is known. The search is case sensitive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the file or directory to search |
required |
Returns:
Type | Description |
---|---|
PortableDeviceContent | None
|
The PortableDeviceContent instance of the child or None if the child could not be found. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()[0]
>>> str(stor.get_child("DCIM"))
'PortableDeviceContent DCIM (1)'
>>> dev[0].close()
Source code in mtp/linux_access.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
get_children()
Get the child items (dirs and files) of a folder.
Returns:
Type | Description |
---|---|
None
|
A Generator of PortableDeviceContent instances each representing a child entry. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()[0]
>>> str(list(stor.get_children())[0])
'PortableDeviceContent Pictures (1)'
>>> dev[0].close()
Source code in mtp/linux_access.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
get_path(path)
Returns a PortableDeviceContent for a child who's path in the tree is known. The path can be fully qualified or starting from the current content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The pathname to the child. |
required |
Returns:
Type | Description |
---|---|
PortableDeviceContent | None
|
The PortableDeviceContent instance of the child or None if the child could not be found. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()[0]
>>> str(stor.get_path(f"{stor.full_filename}/DCIM"))
'PortableDeviceContent DCIM (1)'
>>> str(stor.get_path(f"DCIM"))
'PortableDeviceContent DCIM (1)'
>>> dev[0].close()
Source code in mtp/linux_access.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
remove()
Deletes the current directory or file.
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()[0]
>>> mycont = stor.get_path("DCIM/Camera/test.jpg")
>>> if mycont: _ = mycont.remove()
>>> cont = stor.get_path("DCIM/Camera")
>>> name = './tests/pic.jpg'
>>> cont.upload_file("test.jpg", name)
>>> mycont = stor.get_path("DCIM/Camera/test.jpg")
>>> mycont.remove()
>>> str(mycont)
'PortableDeviceContent test.jpg (2)'
>>> dev[0].close()
Source code in mtp/linux_access.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
upload_file(filename, inputfilename)
Upload of a file to MTP device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Name of the new file on the MTP device |
required |
inputfilename
|
str
|
Name of the file that shall be uploaded |
required |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> stor = dev[0].get_content()[0]
>>> mycont = stor.get_path("DCIM/Camera/test.jpg")
>>> if mycont: _ = mycont.remove()
>>> cont = stor.get_path("DCIM/Camera")
>>> name = './tests/pic.jpg'
>>> cont.upload_file("test.jpg", name)
>>> stor = dev[0].get_content()[0]
>>> mycont = stor.get_path("DCIM/Camera/test.jpg")
>>> str(mycont)
'PortableDeviceContent test.jpg (2)'
>>> dev[0].close()
Source code in mtp/linux_access.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
|
get_content_from_device_path(dev, fpath)
Get the content of a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dev
|
PortableDevice
|
The instance of PortableDevice where the path is searched |
required |
fpath
|
str
|
The pathname of the file or directory |
required |
Returns:
Type | Description |
---|---|
PortableDeviceContent | None
|
An instance of PortableDeviceContent if the path is an existing file or directory else None is returned. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> if os.path.exists(_gvfs_search_path):
... n = "Android_Android_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera"
... else:
... n = "Nokia 6_Nokia 6_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera"
>>> cont = mtp.linux_access.get_content_from_device_path(dev[0], n)
>>> str(cont)
'PortableDeviceContent Camera (1)'
>>> dev[0].close()
Source code in mtp/linux_access.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
get_portable_devices()
Get all attached portable devices.
Returns:
Type | Description |
---|---|
list[PortableDevice]
|
A list of PortableDevice one for each found MTP device. The list is empty if no device was found. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> devs = mtp.linux_access.get_portable_devices()
>>> len(devs) == 1
True
>>> devs[0].close()
Source code in mtp/linux_access.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
makedirs(dev, create_path)
Creates the directories in path on the MTP device if they don't exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dev
|
PortableDevice
|
Portable device to create the dirs on |
required |
create_path
|
str
|
pathname of the dir to create. Any directories in path that don't exist will be created automatically. |
required |
Returns:
Type | Description |
---|---|
PortableDeviceContent
|
A PortableDeviceContent instance for the last directory in path. |
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> if os.path.exists(_gvfs_search_path):
... n = "Android_Android_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera/Test"
... else:
... n = "Nokia 6_Nokia 6_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera/Test"
>>> cont = mtp.linux_access.makedirs(dev[0], n)
>>> str(cont)
'PortableDeviceContent Test (1)'
>>> cont.remove()
>>> dev[0].close()
Source code in mtp/linux_access.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
walk(dev, path, callback=None, error_callback=None)
Iterates ower all files in a tree just like os.walk
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dev
|
PortableDevice
|
Portable device to iterate in |
required |
path
|
str
|
path from witch to iterate |
required |
callback
|
Callable[[str], bool] | None
|
When given, a function that takes one argument (the selected file) and returns a boolean. If the returned value is false, walk will cancel and return empty list. The callback is usefull to show for example a progress because reading thousands of file from one MTP directory lasts very long. |
None
|
error_callback
|
Callable[[str], bool] | None
|
When given, a function that takes one argument (the errormessage) and returns a boolean. If the returned value is false, walk will cancel and return empty list. |
None
|
Returns:
Type | Description |
---|---|
None
|
A tuple with this content:
|
Raises:
Type | Description |
---|---|
IOError
|
If something went wrong |
Examples:
>>> import mtp.linux_access
>>> dev = mtp.linux_access.get_portable_devices()
>>> if os.path.exists(_gvfs_search_path):
... n = "Android_Android_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera"
... else:
... n = "Nokia 6_Nokia 6_PLEGAR1791402808/Interner gemeinsamer Speicher/DCIM/Camera"
>>> for r, d, f in mtp.linux_access.walk(dev[0], n):
... for f1 in f:
... print(f1.name)
...
IMG_20241210_160830.jpg
IMG_20241210_160833.jpg
IMG_20241210_161150.jpg
test.jpg
>>> dev[0].close()
Source code in mtp/linux_access.py
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
|