I am currently writing a very simple script on python which requires that I download a .dat file posted online. I then use loadtxt to plot the data using matplotlib. However, in order to read the file, I had to change directory (os.chdir) to 'downloads' (where the file had been saved). This works fine for me, but I will need to send the script to somebody else, in which case it seems as though the directory would again need to be something else in order to find the file... Where might I save the file so that no matter to whom I send it, the script will run properly?
User-specific path to the dat file can be acquired using:
os.chdir(raw_input('Which is the path to the download folder?'))
Related
I'm working with HPC and am trying to run a python script that reads and modify every tif files in a folder and its subfolders. Right now I'm running a for loop:
files = glob.glob("./Examples/**/*.tif", recursive = True)
for file in files:
But it doesn't modify every file in the subfolder, only the final one. I run the exact same script locally and it worked just fine. So I'm thinking it's something to do with SLURM sitting on top of the code and maybe controlling how and when outputs get written. So could there be a way to write a python/slurm script that looks at the files in the directory and calls a slurm script, passing it a file name? Or any other solutions are also welcomed.
Thanks.
For the past month I've been writing a desktop application for MacOS using Python. It requires opening files and saving compressed data to them. This application creates files using a my own made up extension, so the files are not usable for for other applications. I have almost everything figured out. However, I want to make it so that I can right click on a file with the extension and open it with my python application. I tried using sys.argv to get any arguments for the path of the file to open, but that doesn't work. I know there has to be a way. Preferably there's a builtin variable that is easy to use, but I haven't found anything that helps.
Any help would be useful.
Thanks.
In my project, I have a Python script that scans the source directory and updates a source file with what it finds. I’d like this script to only run when it needs to. At the moment, I have this script in a Run Script build phase with Input Files set to $(PROJECT).xcodeproj/project.pbxproj and Output Files set to the updated source file. This means that the scripts runs when I add new files but it also runs whenever I change project settings. When the script runs unnecessarily, part of the project is recompiled even though none of the source files have changed. This is kind of annoying when all I want to do is tweak some settings.
Is there some way that I can avoid the unnecessary recompilation and just run the script when new source files are added or removed from the project?
I guess I could manually run the script whenever I add or remove a source file.
I think that Xcode is recompiling because the modification date on the file is changed. Python updates the modification date when you flush to a file. So I guess I could just write to the file only when the output is different to the output file. I’m pretty sure reading a file won’t change the modification date. That seems like a lot of fluffing around though. If anyone’s got a better solution, please let me know!
I am facing a strange problem
Whenever my python scripts are creating any csv file, it is making them "Archive".
I mean in properties, Archive check box is checked.
Because of which it can't be read in later part of same script .
How can i create a csv file not archive?
Please help me resolve this problem.
Are you running a Windows OS? If yes, then this is not a problem with the Python CSV library. As about the error encountered while reading the CSV; you may want to re-check your python code for any flaws.
The Archive checkbox is actually an attribute of the file on Windows systems that indicates that the file needs to be backed up. Right click on any other file and you should see "Archive" checked.
Here are a couple of links that would give you more information
MSDN Technet discussion on File Attributes
Wikipedia article on Archive bit
This feels like a simple question, but I don't know if there's a simple answer. I basically just need to make a .py file run when I open up a .txt. I don't really want a loop to check and then run it, preferably i'd like to bundle the txt and the py so all the user sees is a .txt file. Thanks!
EDIT: More Specifically, is there some type of program that can bundle the txt and py so when they double click the txt it runs the py as well as opening the txt file?
This is not as simple as it seems to be. For running a script when a file is being opened you somehow need to recognize the open file event, and I don't know if this would be possible, especially in Windows.
If you want to do something when the file is being modified (saved), you could give the watchdog package a try.
If this does not help, and you know what program is being used for opening the file, you could do it manually: Write a script which first does whatever you need to have done, then opens the text file using subprocess.call.