-2

Hello as the title says i am writing a program which copys data from chrome. my current code is

#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)

i get a error on the line original = r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\History'

i assume the script cannot read %LOCALAPPDATA%\ and needs the correct user path ? what code do i need to input the user directory on a windows PC?

for example 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'
dood
  • 1
  • 2
  • Please post the traceback message with the full error text. – tdelaney Nov 06 '20 at 21:33
  • 1
    Cannot reproduce. It makes sense that there would be an error on `shutil.copyfile` as %LOCALAPPDATA% isn't expanded, but the literal string assignment itself doesn't have a problem that I can see. – tdelaney Nov 06 '20 at 21:36
  • Possible duplicate of https://stackoverflow.com/questions/13184414/how-can-i-get-the-path-to-the-appdata-directory-in-python; I can't vote to close myself without immediately closing the question, though. – chepner Nov 06 '20 at 21:44

2 Answers2

1

Use the pathlib library.

from pathlib import Path

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

shutil.copyfile(origina, target)
chepner
  • 497,756
  • 71
  • 530
  • 681
1

What you want to do is use os.path.expandvars() function on original variable:

os.path.expandvars(r'%LOCALAPPDATA%')

Will return:

C:\Users\USERNAME\AppData\Local

Where USERNAME is the name of the current user.

So, ensure the variables are expanded for this path:

original = os.path.expandvars(original)

before using it in shutil.copy(original, target)

sophros
  • 14,672
  • 11
  • 46
  • 75
  • thank you for the solutions so far i will try them tomorrow and see if there is any progress. – dood Nov 06 '20 at 23:21