Basically I need to password protect a folder, but I've looked around and no software really fits what we need. I have an idea for how I'd go about it, but I can't find any way to check inside python if a folder is currently open in explorer, or watch for a folder being closed. Watchdog and Watcher seem to only be reporting information about files, and if a folder has been created/deleted. Any ideas about how to do this?
I've tried walking through open windows and using their titles to get an idea of what's open, but this doesn't work if a user is inside of a subdirectory. I was also thinking of hooking into explorer with a shell extension, but I have zero clue of where to start with that.
pss' was right. Attempting to rename the file and testing the exception on a set interval does work if a user has a file open. It doesn't work if they just have it open in windows explorer, but that actually doesn't matter now that I think of it. Comes with the added benefit of closing windows the user left open and forgot about.
Related
This may seem trivial, and I admit that it is more a question relating to ease of access in VS Code.
My VS Code window shows "NO FOLDER OPENED". How do I fix this?
Up until the other day when I set VS Code as my default app for .py files (this is the problem), I could see any and all folders, sub-folders, and .py/.ipynb files on my Explorer tab. I am both new to Python and VS Code. I have tried these things to fix my problem:
Attempted to reset to all of microsoft's default app associations--the aforementioned files still open with VS Code.
Attempted to change each file's "Open with:" path (by accessing its properties)--it gives no other option, since PowerShell and CMD are not "apps".
Found this that got close but did not target my specific problem.
Found another article which refers to Windows 7--I didn't read it after seeing this.
I have done various other things, but I think you get the point; I'm at a loss!
This problem seems annoyingly easy to fix. I would rather see if there is some type of setting, in VS Code or Windows, that I am overlooking before searching for a complex work-around.
I'm also new to Stack overflow. I hope this is a "valid" question.
It's important to keep things organized even when you are new to coding. In VScode it is pretty simple to do that. I suggest you to make a folder for your projects and also for other purposes like practicing on examples.
Lets say you created a folder on desktop named Python, and you keep all your .py files in there, then you don't have to open each of the python files by double clicking it, it's a wrong practice. Instead, open VScode application from the start menu or desktop, then go to file > open folder > Python(the python folder you can create on your desktop). This way it opens a workspace(Folder) where you can work with different Python files or any other files.
With Ctrl key pressed, press O and then K. Then, select the folder where your python file is located.
I'm developing a Python script to do file management on Windows. Essentially, I want to be able to move files to another location, and create in their place a shortcut to a Python script which will do intermediate steps before opening the relocated file. I can currently move files, create functioning shortcuts, and set the shortcut icon image. The only thing I have left is to figure out how to detect the icon which is displayed for the file.
-Need to set the icon for a shortcut (which points to a '.py' script)
-This icon should match that of an arbitrary filetype, exe, etc, which I have the path to
-Hopefully done programmatically through Python. os.system() calls are acceptable. Extra credit is given for not requiring additional modules (though I already have win32com, winshell, pythoncom, etc)
Thanks in advance...
File type information is found in the registry. You could look up the file extension's type with the windows registry module.
Edit: It appears icons are available through shell32, as seen here. The Python equivalent seems to be ctypes.windll.shell32.ExtractIconA, though I'm not sure about the syntax.
This is my first time coding a "project" (something more than solving exercises in single files). A number of my .py files have variables imported from a specific path. I also have a main "Run" file where I import things I've written in other files and execute the project as a whole.
Recently I've started working on this project on several different machines (home, work, laptop etc) and have just started learning how to use GitHub.
My question is, how do I deal with the fact that every time I open up my code on a different machine I need to go around changing all the paths to fit the new machine, and then change them back again when I'm home? I started writing a Run file for each location I work at so that my sys.path commands are ok with that machine, but it doesn't solve the problem of my other modules importing variables from specific paths that vary from machine to machine. Is there a way round this or is the problem in how I'm setting up the project itself?
In an ideal world it would all just work without me having to change something before I run, depending on the machine I'm working on, but I don't know if that's possible.
My current thoughts are whether there is some command I'm not aware of that can set variables inside a .py file from my main Run.py file - that way I can just have a run file for each machine.
Any suggestions are gladly taken! Whatever it is, it must be better than commenting back in the correct file path each time I open it on a different machine!
You should always use relative paths, not static which I assume you have got.
Assuming your in an index file and you need to access images folder, you probably have something like /users/username/project/images/image.png
Instead you want something like ../images/image.png, this tells your index file to go backwards one folder to say the root of the project, then proceed into our images folder etc.
Relative paths mean you create a path from where your file exists, and not an entire path from ground up.
You need to identify all your setting that are hardcoded in your project, and make them relative to your current workspace. This is a common problem.
Also if you are using python, make sure that you do not hardcode the path to files in string, but instead that you use the proper system api:
os.path.join('file','to','your','ressource')
that will be computed differently if you are on unix or windows.
I am trying to make a basic program to backup one folder from my memory stick when it is plugged in, (I know there are already programs that can do this but that is no fun!) but am having trouble with permissions.
from shutil import copy2
copy2('F:/Python/Library', 'C:/Users/Torran/Desktop/Python')
This is all I have so far, as I want to get the copying part working before doing the detecting when it is plugged in part. When I run this, however, it keeps giving me a PermissionError...
PermissionError: [Errno 13] Permission denied: 'F:/Python/Library'
I know that a Python script can only access folders in the same folder it is saved to, however this doesn't really help as I need to copy a folder from my memory stick and paste it into a folder on my desktop, so I need a way to give this script access to folders outside the folder it is saved to.
After trying it myself, I found out the problem. You are using the shutils.copy2(src, dst) function on a folder, not a file. src has to be a file. If you are trying to copy a folder to a destination folder, you need to be using shutils.copytree(src, dst).
You end up getting the permission error because shutils.copy2()expects a file.
As for the underlying question of your issue for copying a folder to a destination, please read this for a few different ways to handle this issue.
I would suggest taking a look at "Run python script as admin in Windows", as this answer explains how to force extra admin permissions. Try that, then should it not work the problem will likely lie with the command as cmpgamer says.
By the way, welcome to Python and programming in general! It is a great world to get into as it lets you achieve so much in so many fields. Python is "the" language to know right now as it is very powerful and quick to dev. Have you tried working with the Raspberry Pi? You can do some very fun Python projects on them! Backup, as you are describing, can be achieved with Windows Shell scripting, whereas you can do AI in python!
I've written a program and then used py2exe to convert it to a .exe file. Now im trying to use Resource Hacker to change the icon but when i right click the .exe and use resource hacker all i see is the following:
A folder named PYTHONSCRIPT
and a folder named 24 and thats it.
does anyone know what the issue is here and how to solve it? I've read all the other questions about this and none of them have been a lot of help.
EDIT- Also any alternative way to change the icon on my python script .exe file is also appreciated so by all means if you know an alternative way show me the path!