Chào mừng bạn đến blog Ynghialagi.com Trang Chủ

Table of Content

Bài đăng

How to get desktop path in python Đầy đủ

Mẹo Hướng dẫn How to get desktop path in python Mới Nhất


Bạn đang tìm kiếm từ khóa How to get desktop path in python được Update vào lúc : 2022-02-17 08:13:06 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Ad lý giải và hướng dẫn lại nha.


  • trang chủ

  • python

  • Summary of five methods for Python to obtain windows desktop path


    • 2022-07-18 08:36:07

    • OfStack

    Here introduced 5 python to get window desktop path method, get this path is of what use? 1 is to output the documents generated by the program to the desktop for easy viewing and editing.


    The first two methods are to obtain the absolute path of the current windows desktop through the registry, and the first one is recommended for comparison, because there is no need to install additional extensions, and others can be understood


    1. Use the built-in winreg (recommended)


    import _winreg

    def get_desktop():

    key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r’SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders’)

    return _winreg.QueryValueEx(key, “Desktop”)[0]


    2. win32 extension (third party library required)


    import win32api,win32con

    def get_desktop():

    key =win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,r’SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders’,0,win32con.KEY_READ)

    return win32api.RegQueryValueEx(key,’Desktop’)[0]


    3. win32 extensions are also required


    from win32com.shell import shell, shellcon

    def GetDesktopPath():

    ilist =shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)

    return shell.SHGetPathFromIDList(ilist)


    4. path module of os library built into python


    This method may fail after the user changes the desktop path.


    import os

    def GetDesktopPath():

    return os.path.join(os.path.expanduser(“~”), ‘Desktop’)


    5. Use the socket module (not recommended)


    Of course, this method, Is to get the host name of the current pc, 1 Under normal circumstances, the windows system will set a computer name the beginning of the first time, this computer name will appear in the user directory under the C disk, for example, my computer name is’ jayzhen ‘, then there will be a directory path: C: Users jayzhen, this time my desktop path is: C: Users jayzhen Desktop (the problem is that if you modify the computer name later, this method will not take effect), the code performance is very similar to the fourth


    import socket, os

    def GetDesktopPath()

    hostname = socket.gethostname() #socket.getfqdn(socket.gethostname())

    basepath = os.path.join(“C:Users”,hostname )

    return os.path.join(basepath, ‘Desktop’)

    Reply

    5

    0

    Chia sẻ


    Share Link Tải How to get desktop path in python miễn phí


    Bạn vừa tìm hiểu thêm Post Với Một số hướng dẫn một cách rõ ràng hơn về Clip How to get desktop path in python tiên tiến và phát triển nhất Chia SẻLink Download How to get desktop path in python Free.



    Hỏi đáp vướng mắc về How to get desktop path in python


    Nếu sau khi đọc nội dung bài viết How to get desktop path in python vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha

    #desktop #path #python

    Đăng nhận xét