最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create or copy folders to Windows AppData folders Using Python - Stack Overflow

programmeradmin0浏览0评论

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.

Share Improve this question edited Jan 31 at 19:06 Lucian Thorr asked Jan 31 at 18:54 Lucian ThorrLucian Thorr 2,2671 gold badge26 silver badges33 bronze badges 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

Create 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}")
  1. os.makedirs with exist_ok=True: Creates the folder if it doesn’t exist, and avoids errors if it already exists.
  2. shutil.copytree with dirs_exist_ok=True: Copies the folder and allows overwriting if the destination already exists.
  3. os.getenv('APPDATA'): Retrieves the AppData\Roaming path.
发布评论

评论列表(0)

  1. 暂无评论