python - 将用户主路径地址输入字符串的正确或最佳方法是什么?
问题描述
你好,正如标题所说,我正在编写一个从 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'
解决方案
使用pathlib
图书馆。
from pathlib import Path
original = Path.home() / "Google/Chrome/User data/Default/History"
target = Path("F:/Evidence/Chrome")
shutil.copyfile(origina, target)
推荐阅读
- python - 重塑 np 数组的块
- python - 手动构造日期时间对象时出现意外的时区偏移
- php - 来自 html 周输入的 PHP 日期字符串
- node.js - 您如何获取在 firebase .on 中更新的孩子?
- express - 如何在 MERN 堆栈中发送响应模式?
- devops - jFrog 构建 - 如何不删除生产工件
- c# - 我如何误用 NotThrowAsync
- python - 使用集合中的值命名递归创建的数据框
- javascript - keydown 上的滚动事件,如向下箭头和向上箭头
- node.js - 为什么当 node_modules 包含在卷中时找不到任何开发库?