mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
fix: windows path and encoding adaptation
This commit is contained in:
@@ -76,3 +76,42 @@ def remove_markdown_symbol(text: str):
|
||||
if not text:
|
||||
return text
|
||||
return re.sub(r'\*\*(.*?)\*\*', r'\1', text)
|
||||
|
||||
|
||||
def expand_path(path: str) -> str:
|
||||
"""
|
||||
Expand user path with proper Windows support.
|
||||
|
||||
On Windows, os.path.expanduser('~') may not work properly in some shells (like PowerShell).
|
||||
This function provides a more robust path expansion.
|
||||
|
||||
Args:
|
||||
path: Path string that may contain ~
|
||||
|
||||
Returns:
|
||||
Expanded absolute path
|
||||
"""
|
||||
if not path:
|
||||
return path
|
||||
|
||||
# Try standard expansion first
|
||||
expanded = os.path.expanduser(path)
|
||||
|
||||
# If expansion didn't work (path still starts with ~), use HOME or USERPROFILE
|
||||
if expanded.startswith('~'):
|
||||
import platform
|
||||
if platform.system() == 'Windows':
|
||||
# On Windows, try USERPROFILE first, then HOME
|
||||
home = os.environ.get('USERPROFILE') or os.environ.get('HOME')
|
||||
else:
|
||||
# On Unix-like systems, use HOME
|
||||
home = os.environ.get('HOME')
|
||||
|
||||
if home:
|
||||
# Replace ~ with home directory
|
||||
if path == '~':
|
||||
expanded = home
|
||||
elif path.startswith('~/') or path.startswith('~\\'):
|
||||
expanded = os.path.join(home, path[2:])
|
||||
|
||||
return expanded
|
||||
|
||||
Reference in New Issue
Block a user