Windows download file - detection - python

I am currently writing a python script for a project, but to move on the with the project,
I need to know where all the downloaded files/programs[exe] will go to in Windows.
I know that it will go to download folder of the user's, as long as the user did not change the default location for the downloaded folder, or they did not do save as to a certain location but I am asking if there is a way to locate all the files downloaded regardless of where they are saved?
Any help will be a great help, I have googled but haven't found the answer i was looking for so hoping someone here could provide some insight.

The fact that a file was downloaded once-upon-a-time isn't something you can observe from the filesystem. In other words, no, you can't do this.
However, NTFS does store the fact that a file was JUST downloaded in an alternate data stream (ADS) which you can read. This is how Windows warns you that a file was downloaded from the internet and might be dangerous.
The problem with that is, if the file is ever opened and the user says the file is safe, that data is removed. You can't know that a file was previously downloaded, only if it was downloaded and has never been opened.
If your python script needs to act upon some other files, you should ask the user where the files are, either on the command line or interactively.

There's a few things to consider.
You can always scan the entire drive to find the latest files. It's slow, but possible. That's your worst case scenario.
By extension, you can leverage the Windows file index - Windows Search. This will speed up searching and allow sorting by date, but is still just a faster version of the first option. In other words, it doesn't tell you anything new.
To get the current user's default downloads folder, consider using Windows environment variables, such as: %USERPROFILE%\downloads. This can greatly simplify programmatically finding the current user's folder without having to know their username.
(Edited) I had stated that there was no way to track the origin, but as pointed out in another answer it is possible to tell if it was downloaded using ADS. Specifically, the Zone.Identifier stream signifies that the file came from a different "security zone", i.e. not this computer. Other than that, it doesn't provide details of where it came from, but perhaps that's not important for your use case.

Related

Where do I put downloaded data files?

My Python package needs to download a data file (cc-cedict) that I cannot include in my package because of licensing issues. The file is large enough that I only want to force users to download it once and have it live somewhere in the local filesystem where it can be found by my package. I may want to treat other external data resources in this fashion as well.
What is the recommended way of doing this? Specifically, is there a platform-neutral way of getting a user-writable directory path where such a datafile can be stored and accessed?
I've done enough research to know that the file cannot be stored inside of the package (I guess permissions would make that complicated even if it was allowed generally).

How should I save file directories in python so I won't encounter any problems if I switch to another OS?

so for a long time I was a Windows guy so I just thought adding backslashes while writing directories in python for file manipulation would be okay. I however eventually found out that the UNIX systems use slashes instead and was dumbfounded for a while. That is to say until I discovered os.path.join().
Alas now I have come across a most peculiar circumstance. I need to save a directory of a file into another file such that I can later retrieve it on another system. I'll handle this by splitting the directory string and saving it as a json, but I'm curious if there's a better approach. Any ideas?

Python Pyinstaller - How to Patch Program That I Have Built?

I have built a keyword search tool in Python, and then converted it into an .exe format with Pyinstaller, so people at my workplace could use it without having to install Python.
I have sent this around to everyone by e-mail in a RAR file which they have then saved to their desktops.
I am thinking of some potential tweaks I might make to it, but I do not want to have to keep on sending them the full program every time I decide I want to change something.
So, the question is, is there a way that I can send a patch file to upgrade the program instead? What should I be looking at?
Thanks in advance!
You just can't. Compiling a program freezes it, meaning that it cannot be modified.

Self-updating python Scripts

I wrote 2-3 Plugins for pyload.
Sometimes they change and i let users know over forum that theres a new version.
To avoid that i'd like to give my scripts an auto selfupdate function.
https://github.com/Gutz-Pilz/pyLoad-stuff/blob/master/FileBot.py
Something like that easy to setup ?
Or someone can point me in a direction ?
Thanks in advance!
It is possible, with some caveats. But it can easily become very complicated. Before you know it, your auto-update "feature" will be bigger than the original code!
First you need to have an URL that always contains the latest version. Since you are using github, using raw.githubusercontent might do very well.
Have your code download the latest version from that URL (e.g. using requests), and compare the version with that in the current code. For this purpose I would recommend a simple integer version number, so you don't need any complicated parsing logic.
However, you might want to consider only running that check once per day, or once per week. If you do it every time your file is run, the server might get hammered! So now you have to save a file with the date when the check was last done, and read that to see if it is time to run the check again. This file will need to be saved in a location that you can access on every platform your code is liable to run on. That in itself can be a challenge.
If it is just a single python file, which is installed as the user that is running it, updating is relatively easy. But if the original was installed as root in the global Python directory and your script is running as a nonprivileged user it will be difficult. Especially if it is running as a plugin and cannot ask the user for (temporary) root credentials to install the file.
And what are you going to do if a newer version has more dependencies outside the standard library?
Last but not least, as a sysadmin I don't really like auto-updating software. Especially for critical system infrstructure I like to be able to estimate the consequences before an update.

Why are my pygame images not loading? [duplicate]

This question already has answers here:
pygame.error "couldn't open image.png" only in command prompt
(3 answers)
Closed 2 years ago.
I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame. i found images of the kind he said and what he had. he didn't say where to save them to, i think that may be my problem. here, this is an example of what i have in my program and what shows up on the error
bif="bg.jpg"
mif="images.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((420,315),0,32)
backround=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(backround, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -+ mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
the error reads:
Traceback (most recent call last):
File "C:/Python26/pygame.games/pygame
1", line 10, in <module>
backround=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg
please respond as soon as possible.
Based on that code, your image files need to be saved in the same directory as your game's .py file. Try moving them there.
As a sanity check (to make sure the images will load and it is in fact an issue with supplying the correct path) you could temporarily specify an absolute path to the image in your code, so instead of
bif = "bg.jpg"
You would have
bif = "C:/My_game/images/bg.jpg"
But obviously change the second bit to point to where your image is actually stored. Once you're sure that the image can be loaded using an absolute path, you should use a relative path, and if you're likely to have many assets you should keep them separate from the code, for example by putting them in an assets folder in the same folder as your game code, or similar.
bif = "assets/bg.jpg"
It would be a good idea to check the current working directory during runtime using os.getcwd(). After finding the current working directory, add the assets relative to that path. For example, You could place bg.jpg at <cwd>/python/game/assets/ and write:
bif=r"python/game/assets/bg.jpg"
You may be able to ignore all of this, or only read part depending on what
print pygame.image.get_extended()
take the output for what it's worth, may help you depending on what you get, and one of the following problems.
Try adding the entire path to the directory/folder with the files. If that works either Python is not on Path or you just needed to find that directory/guide Python to it.
Try saving, reading and/or writing to a file. If when saving it automatically assumes the same drive, then your probably good, and need to just guide python to the folder/directory it is located. Their usually isn't a problem if you are saving the .py file in the same exact directory/file as the images.
Do a test, google reading and writing to files, and try to create a simple .txt file.
Also you can use the file save/open/new dialog in Tkinter. See where it assumes to save or Open files to/from.
If Python isn't part of your PATH then there are tons of tutorials and several ways to add it. Some include using the built in native OS GUI's, command prompt/Bash and there are even tools to make it easier, just google how to add python to PATH. You can do a clean uninstall, meaning removing and deleting everything, and then installing it again.
Having two different drives can make things weird. You should see what drive Python is using based on the tests above. Make sure you are saving the current file to the same directory as well as images.
Having multiple versions of Python installed can make things weird. Even if you uninstall Python, sometimes not everything uninstalls in windows, and specifically the versions on Windows market place can mess things up. I would honestly just uninstall, delete and re-install to path if that's the case.
Sometimes using a 3rd party IDLE can actually make things a lot easier. I think the easiest with the PATH type issues is Pycharm. If I am not mistaken it makes you create virtual environments, witch is another way of fixing things (google venv). If all fails then something weird is going on, and you can always remove and uninstall and delete all traces of Python, install it again making sure no traces are left, and it is added to path on install.
The answer about putting the files in the Python library folder is completely wrong. Don't do that, that's pretty much the same as saving it to root. Also how are you going to release anything if it needs you entire Python Library folder, I would have deleted that comment by now if I were that guy, due to sheer embarrassment.
I have had this problem and it was from downloading a version 3.x from the Windows Market place. Somehow it managed to save the Python .exe in the windows folder directly, and other weird things. This made it so frustrating, and hard to fix as it installed it so weird, and I couldn't remember what version I got from the installer and what version from the Marketplace. I would just avoid the Marketplace, I have gotten some janky Windows Sub System, 'Linux' distros also.
Maybe your image is just too large. Make the image smaller first using other image processing application, and try again.
I was just having the same problem. trying to figure out the same tutorial
The way I fixed it.
Put you images in C:\Python26\Lib\site-packages
This is where pygame is installed, and it didn't work until I put the images there.
when declaring your variable for bif and mif, you should write like this;
bif = "C:/Python26/Lib/site-packages/bg.jpg"
with the full location of the image file. NOTICE that the slashes are forwards like so "/"
I tried it with single and double backslashes, like so "\", and "\", but neither worked.
Try all the different ways, and make sure everything is spelled correctly, and you images are where you said they are, and that you got the correct file name (.jpg, .JPG, or .jpeg) You can just look at the properties of the image file.

Categories