简体   繁体   中英

How to refresh a directory (Windows 10) using python

I just need to refresh a folder. A hypothetical ideal example would be:

from aModule import refreshdir # fake
refreshdir("C:\path\to\directory")

Context: I am using Autodesk Desktop Connector, a service that sync data on the cloud with local folders. To avoid expending resources, this tool just checks for new updates when the user opens the file or refresh the directory (so manually). However, in order to automate some operations, I need to refresh the directory with Python. There is no API for this tool.

Thanks in advance! =)

Edit: New files can be added in the cloud. That's why it is important to refresh the folder. Example: Before refreshing: enter image description here After refreshing: enter image description here os.listdir cannot catch those highlited files before refreshing.

Refreshing a directory is not an operating system operation, but a function of the filesystem browser / explorer. A refresh is essentially just reading in the directory contents anew.

Most likely that Adobe tool is hooking into the filesystem functions that do this enumeration of a directory's contents. If this is the case, then the task should be as simple as

import os
os.listdir("C:/path/to/directory")

Keep in mind that backslashes ( \ ) in standard string literals start an escape sequence, ie if you wanted to put an actual backslash there, you'd have to write "\\" . However Windows will happily use forward slashes as directory separator as well, so you can just use that:-)

To solve this problem I created a script in Python using the pywinauto library to do a manually task that clicks on the file and then clicks on the Sync option. In this case you'll need to know the name of the files you want to sync. The code was made to AutoCAD Plant 3D project, you'll need to change the path to your files.

from pywinauto import Application

raiz = "C:\\Users\\YOUR_USERNAME\\ACCDocs\\ORGANIZATION_NAME\\PROJECT_NAME\\Project Files\\PLANT3D_PROJ_NAME\\Plant 3D Models"

Application().start('explorer.exe ' + raiz, timeout=10)

explorer = Application(backend='uia').connect(path='explorer.exe', title="Plant 3D Models")
#Plant3DModels is a variable automatically created with the title of the windows opened
explorer.Plant3DModels.set_focus()

# 'Infra-Geral.dwg' is the name of the file that I will Sync 
file = explorer.Plant3DModels.ItemsView.get_item('Infra-Geral.dwg')
file.right_click_input()
explorer.ContextMenu.Sync.invoke()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM