not allowing me to copy file do to files permission - python-3.x

I have made a program that changes the background picture of my desktop every user defined seconds and there is no problems with that part. However I do have a problem with changing the picture of the log in screen I have set up my computer correctly (a windows 7 computer) to change the background picture (including editing the registry to be able to change the picture more then once).
I currently changing my login picture manually by moving the picture to a folder I created C:\windows\system32\oobe\info\backgrounds. I've chosen to make the task automated using python. I will delete the existing image, copy the new image to the folder and then rename the image to backgroundDefault.jpg and repeat every user defined seconds.
To copy, rename, and delete these files using the os module I've tested these steps in my cmd and it works.
Now what seems to be the problem?
Well I'm able to find the folder using os.path.exists however I'm unable to copy, delete or rename anything because the program doesn't have permission.
It is also worth noting that I have already tried to give my user permission to write in the folder and I already tried to give the python programme administrator access not only the python program but Py.exe and th pyw.exe that sit in the wondows folder
Is there a way to give the program permission or is there another way of changing the folder that I need to move the files to? Or even is there a different snipit of script I could use to achieve a login background change?
def path_writer_bg():
folders_path_bg = input("Please type the folders path of the login background here, then press enter"
"\n>")
if os.path.exists(folders_path_bg):
open("your_path_bg.txt", "w").write(folders_path_bg)
read_folder_path_bg = open("your_path_bg.txt", "r").read()
if os.path.exists("task_bg.txt"):
open("task_bg.txt", "w").write("dir " + read_folder_path_bg + " /s /b >listed_bg.txt")
file_read_task_bg = open("task_bg.txt", "r").readline()
else:
open("task_bg.txt", "w").write("dir " + read_folder_path_bg + " /s /b >listed_bg.txt")
file_read_task_bg = open("task_bg.txt", "r").readline()
os.popen(file_read_task_bg)
else:
input("invalid input. press enter to retry \n")
path_writer_bg()
if os.path.exists("your_path_bg.txt"):
read_folder_path_e_bg = open("your_path_bg.txt", "r").read()
open("task_bg.txt", "w").write("dir " + read_folder_path_e_bg + " /s /b >listed_bg.txt")
file_read_task_e_bg = open("task_bg.txt", "r").readline()
os.popen(file_read_task_e_bg)
else:
path_writer_bg()
def everything_bg():
with open("listed_bg.txt") as file_bg:
num_lines_bg = sum(1 for line_bg in open("listed_bg.txt"))
for num_bg, line_bg in enumerate(file_bg, 1):
rand_line_bg = random.randrange(num_lines_bg - 1)
lines_bg = open("listed_bg.txt", "r").readlines()
open('temp_bg.txt', 'w').writelines(lines_bg[rand_line_bg])
wallpaper_bg()
def wallpaper_bg():
path_bg = open("temp_bg.txt", "r").readline()
if os.path.exists("C:\\Windows\\System32\\oobe\\info\\backgrounds\\backgroundDefault.jpg"):
os.popen("del C:\\Windows\\System32\\oobe\\info\\backgrounds\\backgroundDefault.jpg")
else:
pass
if os.popen("copy /y" + path_bg + " C:\\Windows\\System32\\oobe\\info\\backgrounds"):
os.popen("dir C:\\Windows\\System32\\oobe\\info\\backgrounds /s /b >renamer.txt")
else:
pass
if os.path.exists("renamer.txt"):
rename = open("renamer.txt", "r").readline()
else:
pass
os.popen("rename " + rename + "backgroundDefault.jpg")
wallpaper_bg()
exit()
time.sleep(10)
everything()

Maybe it is because that specific file can't be changed with python due to Microsoft not allowing software to change any file in system32.

Related

Is there any way to configure emulator in Android Studio?

I am doing a project about Android forensics where I need to dump data from android devices so I am using emulator from android studio. using android version 8.1.0. I am following a repo from github here's the link;https://github.com/mspreitz/ADEL
In order to dump file the prerequisite is to add configuration in the xml folder sub directory named https://github.com/mspreitz/ADEL/blob/master/xml/phone_configs.xml
But I cant find any way to configure the emulator...
if anyone could enlighten me will appreciate a lot I have successfully ran the scripts but unable to dump database.the scripts runs ..the code to run '' python adel.py -d emulator-5554'''. I have tried it already but the problem here is the script is unable to dump database for instance: calendar data, contacts data, call logs though I have created these databases and stored it on the emulator when the scripts run it shows
" Calendar database doesn't exist"
" Contacts database doesn't exist"
" database doesn't exist" " mms/sms database doesn't exist"
The code for dumping the contacts database is given :
if float(os_version) < 2.0:
contactsdb_name = "contacts.db"
else:
contactsdb_name = "contacts2.db"
try:
contactsdb = subprocess.Popen(['adb', 'pull', '/data/data/com.android.providers.contacts/databases/' + contactsdb_name, backup_dir], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
contactsdb.wait()
_adel_log.log(contactsdb_name + " -> " +
contactsdb.communicate(0)[1].split("(")[1].split(
")")[0] + " -> " + hashlib.sha256(backup_dir +
"/contacts2.db").hexdigest(), 3)
hash_value.write(contactsdb_name + " -> " + hashlib.sha256(
backup_dir + "/" + contactsdb_name).hexdigest() + " \n")
except:
_adel_log.log( "dumpDBs: ----> contacts
database doesn't exist!", 2)
https://github.com/mspreitz/ADEL/blob/master/_dumpFiles.py this is the link of the full script.
If you config the device as
<phone device="emulator" model="sdk" os_version="8">
<smartphone_information>
...
</phone>
you should be able to use the emulator.
Otherwise post the specific error you are receiving.

How to change the location?

I'm trying to automate image uploading to Instagram using Selenium in Python. I'm successful till opening the fileDialogue but I'm not able to change the directory to where the image is located. It returns an error that ToolbarWindow32 can't be detected by AutoIt.
My code:
ActionChains(browser).move_to_element(browser.find_element_by_xpath(
"/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]/div/button")).click().perform()
handle = f"[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "ToolbarWindow32", photopath) # This line give me the Error
autoit.control_set_text(handle, "Edit1", photopath)
autoit.control_click(handle, "Button1")
Take a look how this is done in _WD_SelectFiles with: https://github.com/Danp2/au3WebDriver/blob/master/wd_helper.au3
You should be able to do the same directly with python+selenium without using AutoIt.
Also take a look on:
https://github.com/Danp2/au3WebDriver/blob/master/wd_demo.au3
There is example how directly in AutoIt it is possible to do the same with WebDriver UDF without opening any FileOpenDialog :
Func DemoUpload()
; REMARK This example uses PNG files created in DemoWindows
; navigate to "file storing" website
_WD_Navigate($sSession, "https://www.htmlquick.com/reference/tags/input-file.html")
; select single file
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//section[#id='examples']//input[#name='uploadedfile']", #ScriptDir & "\Screen1.png")
; select two files at once
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[#name='uploadedfiles[]']", #ScriptDir & "\Screen1.png" & #LF & #ScriptDir & "\Screen2.png")
; accept/start uploading
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[2]")
_WD_ElementAction($sSession, $sElement, 'click')
EndFunc ;==>DemoUpload

retrieving files from ftp to specific os directory using python 3

I'm trying to automate a daily ftp transfer using a Python3 script. I'm having a small issue though with writing the files were i want them to be. This is what I'm doing:
import time, os
from ftplib import FTP
from datetime import datetime
today=time.strftime('%d%m%y')
dirName='mydir'+today
if not os.path.exists(dirName):
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
else:
print("Directory " , dirName , " already exists")
os.chdir(dirName)
start = datetime.now()
ftp = FTP('ftp')
ftp.login('user','pass')
ftpdir='localdir'+today
ftp.cwd(ftpdir)
# Get All Files
files = ftp.nlst()
# Print out the files
for file in files:
print("Downloading..." + file)
ftp.retrbinary("RETR " + file, open(dirName + file, 'wb').write)
ftp.close()
what I get with this code is that all the downloaded ftp files stay in the folder level above "today" while their filename start with the "today" str.
Can someone give a hand here please.
Thanks in advance
You have to separate the path components. For platform independent solution, use os.path.join:
import os
dirName = os.path.join('mydir', today)
Solved the issue with a bar:
# Print out the files
for file in files:
print("Downloading..." + file)
ftp.retrbinary("RETR " + file, open(dirName + '\\' + file, 'wb').write)
ftp.close()

WindowsError: [Error2] The system cannot find the file specified

I expect the program to open the link specified and then close the browser after some time.REPEAT this task 3 times.
But I end up getting the mentioned error.
import time
import subprocess
total_breaks = 3
break_count = 0
print("This program started on " + time.ctime())
while(break_count < total_breaks):
browser = subprocess.Popen(['firefox', 'https://www.google.com/'])
sleep(10)
browser.terminate()
break_count = break_count + 1
You need to specify the path to firefox Just like the error shows:
WindowsError: [Error2] The system cannot find the file specified
subprocess.Popen([r'C:\Program Files\Mozilla Firefox\Firefox.exe',
'-new-tab', 'http://www.google.com/'])

How to put multiple documents into Berkeley-DB XML container?

I have a directory with a bunch of XML documents and want to put all of them into a container.
In other words, I need to do something like this:
dbxml> putDocument tests/*.xml
I have written a GUI program to do that but the host server does not have X-windows installed, so must be in command line.
I do a similar thing when reloading certain XML docs into my current application DB. It helps if all of the files sharing a common naming convention. In python you would could use the following script to add doc001.xml to doc009.xml:
from bsddb3.db import *
from dbxml import *
#Load source files 001 - 009
sourceDir = 'C:/directory-containing-xml-docs'
fileRange = range(1,10)
for x in fileRange:
mycontainer = mymgr.openContainer("myDB.dbxml")
xmlucontext = mymgr.createUpdateContext()
xmlinput = mymgr.createLocalFileInputStream(sourceDir + "doc00" + str(x) + ".xml")
mycontainer.putDocument("doc00" + str(x) + ".xml", xmlinput, xmlucontext)
print 'Added: ' + str(x)
del mycontainer
print '1 - 9 Added'
Hope that helps
You could have a shell script write the list of XML files to another file and then call dbxml_load_container with the -f option.
Ended up using a script that lists files and puts everything into the DB.

Resources