How to close a specific folder with Python - python

I am trying to make a simple program that closes a specific folder every time it gets opened to deny access to said folder. I've tried using VBScript , and the codes I was given worked at first, but they failed after some circumstances (like trying to open the specific folder a lot of times in quick succession, or sometimes even after closing an unrelated folder).
The folder I'm trying to close is C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

Related

Can't move a folder despite it not being used - Windows | Python

I have encountered an issue where I have a file in Onedrive that I am not able to move. I have determined that, despite it saying that the action cant be completed because the folder or file is open in another program, it is not being used by another program.
I have come to this conclusion by using resource monitor to check which apps are using the folder. There are none besides file explorer.
Context as it's probably important:
The reason why this has happened is that i deleted a python venv in the file directory, resulting in my VS Code interpreter breaking. I have restored this venv file but the issue still has not been fixed. According to this post, it is the result of workspace links, however, I have never once used workspaces. The general idea that a file WITHIN that unmovable directory could be the reason though, and as a result, is possibly why this issue may be occuring. What does not make sense is why it is still happening even without VS Code running or any service connections being made as evident through resource monitor
Another thing to note is that the folder has not completely synced, however, based on past experience, this should not be an issue.
I successfully moved the file via terminal, however the issue still remains about why i could not simply move the file with file explorer.

EDIT: What might be causing the weird differences the directory the python interpreter uses as home directory in similar installations?

I have a short script written in python that saves a simple .txt to the same directory the .py file is in. Problem is: It only does so on one of my two computers.
My code doesn't include a hard-coded path to write to. On my laptop, I can put the makemeanote.py in any folder and it will create the note right there. On my desktop pc, all the notes end up in System32. PATH is set exactly the same way on both machines, and both use Windows\py.exe as the executable.
Somewhat interestingly, I only get an admin-screen on the desktop pc, asking if I want to allow changes to my system by "Built: Release_master_v3.8.2_"etc., whereas on my laptop it simply runs and does its job.
No amount of un- and reinstalling has changed anything, even when I thought I had eradicated any trace of python on my hard drive. Both PCs use current Win10/64 installations.
What is happening there?
A clarification: It's not about fixing the bug, it's about understanding the inconsistent behaviour! I know I could just hard-code any directory, but that takes away the beauty, don't you think?
I finally found the cause of the weird behavior:
For some reason, the py.exe in my WINDOWS folder was set to always be run as admin. As soon as I unchecked that option, the User Account Control (UAC) check disappeared and my file finally behaved as I had expected.

Force to open pdf in browser, not downloading

I am working with Python 3.6.6 and need to display several pdf-files in the browser.
I tried several pdf files and got 2 different results.
For example:
import webbrowser
webbrowser.open('https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf')
opens a new tab in my browser and displays the file.
Other files are downloaded immediatelly, instead of displaying in a new tab in the browser.
I want to know the reason for, why some files are downloaded automatically and others are displayed in the browser, where I can manually download the file.
I need a solution where I can force the file to open in the browser.
I already read through this question but it seems it is more restricted to HTML.
One last thing I noticed. If I try to download any pdf file from the internet it opens the file in a new tab. The problem with downloading them only appears with files which are saved on a webserver within my company.
Is this just a missing configuration on the webserver?
If yes, is it anyhow possible to get around this configuration using python?
It's the web-browser's decision whether to put the file in the downloads directory (and not display it) or whether to cache the file and open it in-browser. As such, do not use the web-browser installed on the end-user's system.
Instead, have your program include its own web-browser. Open the pdf (and everything else your program displays) in your program's web-browser. That way, you can always modify your program's web-browser to view files in-browser instead of saving to the downloads directory.
Several free open-source web-browsers are available on the market. Just use one of them.

Can you stop PyCharm from automatically closing script files when you click out of the program?

I am having a problem with PyCharm (Community Edition) in that, when I open a .py file in the program, I can happily read and write in the file as usual, however when I click out of PyCharm (to look my emails for example) and then click back into it to carry on with my code, the file automatically closes and the project tree structure collapses (so I have to re-open it every time).
So far I have tried changing the tab limit to a high number, but this doesn't seem to help (and it shouldn't be related, since this happens when I open just one file).
I had the same issue before. I'm assuming you're connected to a network shared folder via UNC path (e.g. \\foo\bar\)?
If so, it is not currently supported. You'll need to map your network folder and give it a Drive letter. Then load up your project using the mapped drive, and it'll work like a charm.
If that's not the scenario though, please give us more information.

Why does Task Manager not run some lines of code in script?

Python novice here.
I have a Python script that performs some geodatabase management (reconcile/post versions, compress, etc). I have the following line of code in my script:
createLog = open(str(datetime.date.today()) + ".txt", "w")
along each step of the script I add to the text file with the following statements:
createLog.write("Database connections blocked.\n")
When I run the script in my IDE (PyCharm) I get the desired result: A text file with each step written to the .txt file. When I run it in Task Scheduler no .txt file is created and therefore no log. Everything else runs as far as I can tell. I'm able to track edits made to the data.
I have experienced things like this before with task scheduler but have never been able to resolve the problem in the past.
Any ideas?
I think this is a working directory problem. Python's open function opens a file in the current working directory, NOT in the same folder as the script. This is a common misconception! (Which confused me for ages when learning Python...)
So what is a working directory? Well to quote my good friend Wikipedia:
In computing, the working directory of a process is a directory of a hierarchical file system, if any,[1] dynamically associated with each process. When the process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the current working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.
(Source: https://en.wikipedia.org/wiki/Working_directory)
So how is this working directory selected?
Well it is selected by the parent process of that processes! When you run a program from a shell like bash, the shell (the parent process) helpfully sets the working directory of the program you are running (the child process) to the directory you are currently in. (That is, the directory you cd'd to.)
Since your IDE is smart and helpful, it is starting your Python script process and setting the working directory to the same place the script itself is located. The task scheduler is less helpful... I have absolutely no idea what it is setting the working directory to. However if you search your system, I am sure you will find the log file lying about somewhere!

Categories

Resources