Posizionare correttamente le cartelle di Windows è un po 'un lavoro di routine in Python. In base alle risposte relative alle tecnologie di sviluppo Microsoft, ad esempio this one, è necessario ottenerle utilizzando Vista Known Folder API. Questa API non è racchiusa dalla libreria standard Python (sebbene ci sia an issue from 2008 che la richiede), ma si può usare il modulo ctypes per accedervi comunque.
adattamento della risposta di cui sopra utilizzare l'ID della cartella per i download shown here e combinandolo con il codice UNIX esistente dovrebbe risultare in codice che assomiglia a questo:
import os
if os.name == 'nt':
import ctypes
from ctypes import windll, wintypes
from uuid import UUID
# ctypes GUID copied from MSDN sample code
class GUID(ctypes.Structure):
_fields_ = [
("Data1", wintypes.DWORD),
("Data2", wintypes.WORD),
("Data3", wintypes.WORD),
("Data4", wintypes.BYTE * 8)
]
def __init__(self, uuidstr):
uuid = UUID(uuidstr)
ctypes.Structure.__init__(self)
self.Data1, self.Data2, self.Data3, \
self.Data4[0], self.Data4[1], rest = uuid.fields
for i in range(2, 8):
self.Data4[i] = rest>>(8-i-1)*8 & 0xff
SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath
SHGetKnownFolderPath.argtypes = [
ctypes.POINTER(GUID), wintypes.DWORD,
wintypes.HANDLE, ctypes.POINTER(ctypes.c_wchar_p)
]
def _get_known_folder_path(uuidstr):
pathptr = ctypes.c_wchar_p()
guid = GUID(uuidstr)
if SHGetKnownFolderPath(ctypes.byref(guid), 0, 0, ctypes.byref(pathptr)):
raise ctypes.WinError()
return pathptr.value
FOLDERID_Download = '{374DE290-123F-4565-9164-39C4925E467B}'
def get_download_folder():
return _get_known_folder_path(FOLDERID_Download)
else:
def get_download_folder():
home = os.path.expanduser("~")
return os.path.join(home, "Downloads")
Un modulo più completa per il recupero di cartelle noti da Python è available on github.
Con sufficiente ctype-foo è possibile adattare [il codice specifico di Windows in questa risposta] (http://stackoverflow.com/a/7672816/1600898) a Python (con un fallback a 'os.expanduser (...)' su piattaforme non Windows). [Qui] (http://stackoverflow.com/a/29888752/1600898) è un esempio. Si noti, tuttavia, che una directory "Download" non è un concetto indipendente dalla piattaforma. È abbastanza possibile incontrare sistemi Linux che non ne hanno uno, quindi assicurati di crearlo se non esiste. – user4815162342
@ user4815162342: Ok, grazie per il consiglio con una directory "Download" non è un concetto indipendente dalla piattaforma.! –