首页 > 解决方案 > 将用户主路径地址输入字符串的正确或最佳方法是什么?

问题描述

你好,正如标题所说,我正在编写一个从 chrome 复制数据的程序。我目前的代码是

#library imports
import sys
import shutil
import os
import subprocess


#search and find requried files.

os.path.expanduser('~user') #search for user path

#Making directory
newpath = r'F:\Evidence\Chromium'
newpath = r'F:\Evidence\Chrome'

#Copy chrome file
original = r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\History'
target = r'F:\Evidence\Chrome'
shutil.copyfile(original, target)

我在线上遇到错误original = r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\History'

我假设脚本无法读取%LOCALAPPDATA%\并且需要正确的用户路径?我需要什么代码才能在 Windows PC 上输入用户目录?

例如C:\Users\(Script to find out this part)\AppData\Local\Google\Chrome\User Data\Default

Scripv3.py", line 25, in <module>
shutil.copyfile(original, target)
File "C:\Users\darks\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '%LOCALAPPDATA%\\Google\\Chrome\\User Data\\Default\\History'

标签: pythonpath

解决方案


使用pathlib图书馆。

from pathlib import Path

original = Path.home() / "Google/Chrome/User data/Default/History"
target = Path("F:/Evidence/Chrome")

shutil.copyfile(origina, target)

推荐阅读