Using standard library Python functions like os.makedirs
or shutil.copy2
, I can put folders inside the %HOME%/AppData/Roaming/MyApp
folder but they are invisible to Explorer, Powershell and any other app.
I know they exist because I can see them via Python using os.path.exists
.
Can someone explain why and/or how to create folders that are accessible using standard library? I have a solution by calling out to subprocess.run(["powershell ..."])
but that's 100% hacky in my opinion.
Using standard library Python functions like os.makedirs
or shutil.copy2
, I can put folders inside the %HOME%/AppData/Roaming/MyApp
folder but they are invisible to Explorer, Powershell and any other app.
I know they exist because I can see them via Python using os.path.exists
.
Can someone explain why and/or how to create folders that are accessible using standard library? I have a solution by calling out to subprocess.run(["powershell ..."])
but that's 100% hacky in my opinion.
- AppData is a hidden folder. Make them visible in Explorer with View > Show > Hidden Items. – Hans Passant Commented Jan 31 at 19:32
- @HansPassant, nope. Not it either. It's starting to feel like maybe this is something specific to my company's machines. Like an anti-virus protection or something. – Lucian Thorr Commented Jan 31 at 19:34
- stackoverflow/questions/483840/… – Hans Passant Commented Jan 31 at 19:40
1 Answer
Reset to default 0Create or copy folders to the AppData
directory in Python
Example code:
import os
import shutil
# Get AppData path and create/copy folders
appdata_path = os.getenv('APPDATA')
myapp_path = os.path.join(appdata_path, 'MyApp')
# Create folder if it doesn't exist
os.makedirs(myapp_path, exist_ok=True)
# Copy a folder to AppData
source_folder = 'path/to/source/folder'
destination_folder = os.path.join(myapp_path, 'destination_folder')
if os.path.exists(source_folder):
shutil.copytree(source_folder, destination_folder, dirs_exist_ok=True)
else:
print(f"Source folder does not exist: {source_folder}")
print(f"Folders created/copied to: {myapp_path}")
os.makedirs
withexist_ok=True
: Creates the folder if it doesn’t exist, and avoids errors if it already exists.shutil.copytree
withdirs_exist_ok=True
: Copies the folder and allows overwriting if the destination already exists.os.getenv('APPDATA')
: Retrieves theAppData\Roaming
path.