access
mtp_access Documentation
A module to access Mobile Devices from Windows via USB connection.
Author: Heribert Füchtenhans
Version: 1.1.0
Implements access to basic functions of the Windows WPD API Yes I know, there are a lot of pylint disable and type ignors :-) For examples please look into the tests directory.
Requirements: OS: Windows 10 Python: comtypes
The module contains the following functions:
- 'get_portable_devices' Get all attached portable devices.
- 'get_content_from_device_path' - Get the content of a path.
- '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:
- 'PortableDeviceContent' - Class for one file, directory or storage
- 'PortableDevice' - Class for one portable device found connected
Examples:
>>> import win_mtp.mtp_access
>>> win_mtp.mtp_access.get_portable_devices()
[<PortableDevice: ('HSG1316', 'HSG1316')>]
PortableDevice
Class with the infos for a connected portable device. The instanzes of this class will be created internaly. User should not instanciate them manually until you know what you do.
Public methods
get_description get_content
Public attributes:
Raises:
Type | Description |
---|---|
COMError
|
If something went wrong |
Source code in win_mtp\access.py
683 684 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 744 745 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 |
|
__init__(p_id)
Init the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_id |
str
|
ID of the found device |
required |
Source code in win_mtp\access.py
698 699 700 701 702 703 704 705 706 707 |
|
get_content()
Get the content of a device.
Returns:
Type | Description |
---|---|
PortableDeviceContent
|
An instance of PortableDeviceContent |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> str(dev[0].get_content())[:33]
'<PortableDeviceContent c_wchar_p('
Source code in win_mtp\access.py
774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
get_description()
Get the name and the description of the device. If no description is available name and description will be identical.
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
A tuple with of name, description |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> dev[0].get_description()
('HSG1316', 'HSG1316')
Source code in win_mtp\access.py
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 744 745 746 747 748 749 750 751 752 753 754 |
|
PortableDeviceContent
Class for one file, directory or storage with it's properties. This class is only internaly created, use it only to read the properties
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object_id |
Any
|
MTP object id. |
required |
content |
PortableDeviceContent
|
interface to IPortableDeviceContent. |
required |
properties |
Optional[Any]
|
The interface that is required to get or set properties on an object on the device. |
None
|
Public methods
get_properties get_children get_child get_path create_content upload_stream upload_file download_stream download_file remove
Public attributes
name: Name on the MTP device fullname: The full path name date_created: The file date size: The size of the file in bytes content_type: Type of the entry. One of the WPD_CONTENT_TYPE_ constants
Raises:
Type | Description |
---|---|
COMError
|
If something went wrong |
Source code in win_mtp\access.py
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 234 235 236 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 638 639 640 641 642 643 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 |
|
__init__(object_id, content, properties=None)
Source code in win_mtp\access.py
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 234 235 236 237 238 239 240 241 242 |
|
__repr__()
Source code in win_mtp\access.py
449 450 451 |
|
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 |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> mycont = cont.get_path("Interner Speicher\Music\MyMusic")
>>> if mycont: _ = mycont.remove()
>>> cont = cont.get_path("Interner Speicher\Music")
>>> cont.create_content("MyMusic")
Source code in win_mtp\access.py
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 |
|
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 |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> cont = cont.get_path("Interner Speicher\Ringtones\hangouts_incoming_call.ogg")
>>> name = '..\..\Tests\hangouts_incoming_call.ogg'
>>> cont.download_file(name)
Source code in win_mtp\access.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
download_stream(outputstream)
Download a file from MTP device. The used ProtableDeviceContent instance must be a file! For easier usage use download_file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outputstream |
Any
|
Open python file for writing |
required |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> cont = cont.get_path("Interner Speicher\Ringtones\hangouts_incoming_call.ogg")
>>> name = '..\..\Tests\hangouts_incoming_call.ogg'
>>> outp = open(name, "wb")
>>> cont.download_stream(outp)
>>> outp.close()
Source code in win_mtp\access.py
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 |
|
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 |
---|---|
Optional[PortableDeviceContent]
|
The PortableDeviceContent instance of the child or None if the child could not be |
Optional[PortableDeviceContent]
|
found. |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> str(cont.get_child("Interner Speicher"))[:58]
"<PortableDeviceContent s10001: ('Interner Speicher', 0, -1"
Source code in win_mtp\access.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
|
get_children()
Get the child items of a folder.
Returns:
Type | Description |
---|---|
list[PortableDeviceContent]
|
A list of PortableDeviceContent instances each representing a child entry. |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> str(cont.get_children()[0])[:58]
"<PortableDeviceContent s10001: ('Interner Speicher', 0, -1"
Source code in win_mtp\access.py
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 |
|
get_path(path)
Returns a PortableDeviceContent for a child whos path in the tree is known
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The pathname to the child. Each path entry must be separated by the os.path.sep character. |
required |
Returns:
Type | Description |
---|---|
Optional[PortableDeviceContent]
|
The PortableDeviceContent instance of the child or None if the child could not be |
Optional[PortableDeviceContent]
|
found. |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> str(cont.get_path("Interner Speicher\Android\data"))[:41]
"<PortableDeviceContent oE: ('data', 1, -1"
Source code in win_mtp\access.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
get_properties()
Get the properties of this content.
Returns:
Name | Type | Description |
---|---|---|
name |
str
|
The name for this content, normaly the file or directory name |
content_type |
int
|
One of the content type values that descripe the type of the content WPD_CONTENT_TYPE_UNDEFINED, WPD_CONTENT_TYPE_STORAGE, WPD_CONTENT_TYPE_DIRECTORY, WPD_CONTENT_TYPE_FILE, WPD_CONTENT_TYPE_DEVICE |
size |
int
|
The size of the file or 0 if content ist not a file |
date_created |
datetime
|
The reation date of the file or directory |
capacity |
int
|
The capacity of the storage, only valid if content_type is WPD_CONTENT_TYPE_STORAGE |
free_capacity |
int
|
The free capacity of the storage, only valid if content_type is WPD_CONTENT_TYPE_STORAGE |
serialnumber |
str
|
The serial number of the device, only valid if content_type is WPD_CONTENT_TYPE_DEVICE |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> cont.get_properties()
('HSG1316', 0, -1, datetime.datetime(1970, 1, 1, 0, 0), -1, -1, 'DQVSSCM799999999')
Source code in win_mtp\access.py
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 |
|
remove()
Deletes the current directory or file.
Return
0 on OK, else a windows errorcode
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> mycont = cont.get_path("Interner Speicher\Music\Test.mp3")
>>> if mycont: _ = mycont.remove()
>>> cont = cont.get_path("Interner Speicher\Music")
>>> name = '..\..\Tests\OnFire.mp3'
>>> cont.upload_file("Test.mp3", name)
>>> cont = dev[0].get_content()
>>> mycont = cont.get_path("Interner Speicher\Music\Test.mp3")
>>> mycont.remove()
0
Source code in win_mtp\access.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 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 |
|
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 |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> mycont = cont.get_path("Interner Speicher\Music\Test.mp3")
>>> if mycont: _ = mycont.remove()
>>> cont = cont.get_path("Interner Speicher\Music")
>>> name = '..\..\Tests\OnFire.mp3'
>>> cont.upload_file("Test.mp3", name)
Source code in win_mtp\access.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
|
upload_stream(filename, inputstream, stream_len)
Upload a steam to a file on the MTP device. For an easier usage use upload_file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
Name of the new file on the MTP device |
required |
inputstream |
Any
|
open python file |
required |
stream_len |
int
|
length of the file to upload |
required |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> cont = dev[0].get_content()
>>> mycont = cont.get_path("Interner Speicher\Music\Test.mp3")
>>> if mycont: _ = mycont.remove()
>>> cont = cont.get_path("Interner Speicher\Music")
>>> name = '..\..\Tests\OnFire.mp3'
>>> size = os.path.getsize(name)
>>> inp = open(name, "rb")
>>> cont.upload_stream("Test.mp3", inp, size)
>>> inp.close()
Source code in win_mtp\access.py
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 |
|
get_content_from_device_path(dev, path)
Get the content of a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dev |
PortableDevice
|
The instance of PortableDevice where the path is searched |
required |
path |
str
|
The pathname of the file or directory |
required |
Returns:
Type | Description |
---|---|
Optional[PortableDeviceContent]
|
An instance of PortableDeviceContent if the path is an existing file or directory else None is returned. |
Raises:
Type | Description |
---|---|
COMError
|
If something went wrong |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> n = "HSG1316\Interner Speicher\Ringtones"
>>> w =win_mtp.mtp_access.get_content_from_device_path(dev[0], n)
>>> str(w)[:46]
"<PortableDeviceContent o3: ('Ringtones', 1, -1"
Source code in win_mtp\access.py
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 |
|
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 |
---|---|
COMError
|
If something went wrong |
Examples:
>>> import win_mtp.mtp_access
>>> win_mtp.mtp_access.get_portable_devices()
[<PortableDevice: ('HSG1316', 'HSG1316')>]
Source code in win_mtp\access.py
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 827 828 829 |
|
makedirs(dev, 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 |
path |
str
|
pathname of the dir to create. Any directoriues in path that don't exist will e created automatically. |
required |
Exceptions: comtypes.COMError: If something went wrong
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> n = "HSG1316\Interner Speicher\Music\MyMusic\Test1"
>>> str(win_mtp.mtp_access.makedirs(dev[0], n))[:22]
'<PortableDeviceContent'
Source code in win_mtp\access.py
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
|
walk(dev, path)
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 |
Returns:
Type | Description |
---|---|
Generator[tuple[str, list[PortableDeviceContent], list[PortableDeviceContent]], None, None]
|
A tuple with this content: A string with the root directory A list of PortableDeviceContent for the directories in the directory A list of PortableDeviceContent for the files in the directory |
Raises:
Type | Description |
---|---|
COMError
|
If something went wrong |
Examples:
>>> import win_mtp.mtp_access
>>> dev = win_mtp.mtp_access.get_portable_devices()
>>> n = "HSG1316\Interner Speicher\Ringtones"
>>> for r, d, f in win_mtp.mtp_access.walk(dev[0], n):
... for f1 in f:
... print(f1.name)
...
hangouts_message.ogg
hangouts_incoming_call.ogg
Source code in win_mtp\access.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|