I've recently started working with python. In another topic, I read that someone had to create temporary files, has he had to create file (also in python).
So my question is, what is the interest of working with temp files ?
To me it would be, not to have too many (unneeded files, that can be removed later).
In my project, I have a main file
main.dat
In which I extract 2 blocks,
main_first_block.dat
main_second_block.dat
And then I combine main_second_block.dat and main_second_block.dat
final_file.dat
So it makes a total of 4 files.
And at the end, I dont need main_first_block.dat and main_second_block.dat anymore.
I only need to keep main_file.dat and final_file.dat
So my question is, should I create tmp files, or delete the unneeded files at the end of my script ?
Thanks guys for your enlightenment ;)
There is not really that much a difference in lines of code.
Yet, tmp files indicate that they are not made to stay. Therefore they bring more clarity to your code.
When using tmp files make sure to avoid the mktemp()-function
as it is highly vulnerable to attacks.
Hope I could help in some way.
Related
first up, please forgive me if I'm making an obvious mistake, I'm very new to this lol.
I've made a little trivia game for me and my friends and I'm trying to keep a scoreboard using Configparser. Currently, what I think I'm doing is when someone gets a question correct, I read the file called scoreboard.ini grab that person's score add 1 to it and then rewrite the file. My issue is that the changes to the file don't save. When I run it and later call the scores it gives me the original unedited version and opening the file itself also shows that nothing has changed.
with open(r'/filepath/scoreboard.ini','r+') as files:
config.read((r'/filepath/scoreboard.ini'))
print(config.getint('scores','playerA')) ### returns 0 which is correct
PlayerA_Score = config.getint('scores','PlayerA_Score') + 1
print(PlayerA_Score) ### returns 1 - working correctly
with open(r'/filepath/scoreboard.ini','w+') as files:
config.write(files)
config.set('scores', 'playerA', PlayerA_Score)
print(config.getint('scores', 'playerA')) ### also returns 1 - still working up to here
os.rename('scoreboard.ini','scoreboard.ini')
So up to here, it seems to be working fine, when I print out the values. Yet if I open the scoreboard file it still has the original values.
Also worth mentioning I'm renaming the file as the same thing cause that was one potential solution I saw suggested elsewhere, but it hasn't worked. Previously I was just closing the file at that point instead.
I've also tried having two files, scoreboard which I open and read the values from and then writing a new file called scoreboardA where I write the updated scores values to. Then renaming scoreboardA to scoreboard and renaming scoreboard to something else. Another potential solution I saw online that didn't work and honestly just confused me.
I'm really at my wits end with this one lol and only have like 3 weeks of coding under my belt so any help would be very appreciated. Apologies again if this is a simple question I couldn't find a solution anywhere.
Other potentially relevant information:
I am doing this for a bot I'm making for discord using discord.py
I am working in Pycharm
I am on a mac
If you need any other information please let me know.
Thanks!
Finally realized what I was doing wrong. It seems I misunderstood what config.write() does lol. I needed to open the file, change the values using set and then use write() to save that to my file. My amended code is.
with open(r'/filepath/scoreboard.ini','r+') as files:
config.read((r'/filepath/scoreboard.ini'))
print(config.getint('scores','playerA'))
PlayerA_Score = config.getint('scores','PlayerA_Score') + 1
print(PlayerA_Score)
config.set('scores', 'playerA', PlayerA_Score)
print(config.getint('scores', 'playerA'))
with open(r'/filepath/scoreboard.ini','w+') as files:
config.write(files)
I might still be misunderstanding something but my code works so maybe this will fix it for anyone else having a similar issue! :)
I have seen a similar question on this site but not answered correctly for my requirements. I am reasonably familiar with py2exe.
I'd like to create a program (in python and py2exe) that I can distribute to my customers which would enable them to add their own data (not code, just numbers) and redistribute as a new/amended exe for further distribution (as a single file, so my code + data). I understand this can be done with more than one file.
Is this conceptually possible without my customers installing python? I guess I'm asking how to perform the 'bundlefiles' option?
Many thanks
I think it is possible. I'm not sure how py2exe works, but I know how pyinstaller does and since both does the same it should work similiar.
Namely, one-file flag doesn't really create one file. It looks like that for end user, but when user run app, it unpacks itself and files are stored somewhere physically. You could try to edit some source file (ie numbers.py, or data.py) and pack it again with changed data.
I know it's not the best explanation, you have to think further on your own. I'm just showing you the possible way.
Is it possible for the user to input a specific directory on their computer, and for Python to write the all of the file/folder names to a text document? And if so, how?
The reason I ask is because, if you haven't seen my previous posts, I'm learning Python, and I want to write a little program that tracks how many files are added to a specific folder on a daily basis. This program isn't like a project of mine or anything, but I'm writing it to help me further learn Python. The more I write, the more I seem to learn.
I don't need any other help than the original question so far, but help will be appreciated!
Thank you!
EDIT: If there's a way to do this with "pickle", that'd be great! Thanks again!
os.walk() is what you'll be looking for. Keep in mind that this doesn't change the current directory your script is executing from; it merely iterates over all possible paths that stem from the arguments to it.
for dirpath, dirnames, files in os.walk(os.path.abspath(os.curdir)):
print files
# insert long (or short) list of files here
You probably want to be using os.walk, this will help you in creating a listing of the files down from your directory.
Once you have the listing, you can indeed store it in a file using pickle, but that's another problem (i.e.: Pickle is not about generating the listing but about storing it).
While the approach of #Makato is certainly right, for your 'diff' like application you want to capture the inode 'stat()' information of the files in your directory and pickle that python object from day-to-day looking for updates; this is one way to do it - overkill maybe - but more suitable than save/parse-load from text-files IMO.
I have never used Python before, most of my programming has been in MATLAB and Unix. However, recently I have been given a new assignment that involves fixing an old PyEPL program written by a former employee (I've tried contacting him directly but he won't respond to my e-mails). I know essentially nothing about Python, and though I am picking it up, I thought I'd just quickly ask for some advice here.
Anyway, there are two issues at hand here, really. The first is this segment of the code:
exp = Experiment()
exp.setBreak()
vt = VideoTrack("video")
at = AudioTrack("audio")
kt = KeyTrack("key")
log = LogTrack("session")
clk = PresentationClock()
I understand what this is doing; it is creating a series of tracking files in the directory after the program is run. However, I have searched a bunch of online tutorials and can't find a reference to any of these commands in them. Maybe I'm not searching the right places or something, but I cannot find ANYTHING about this.
What I need to do is modify the
log = LogTrack("session")
segment of the code, so that all of the session.log files go into a new directory, separate from the other log files. But I also need to find a way to not only concatenate them into a single session.log file, but add a new column to that file that will add the subject number (the program is meant to be run by multiple subjects to collect data).
I am not asking anyone to do my work for me, but if anyone could give me some pointers, or any sort of advice, I would greatly appreciate it.
Thanks
I would first check if there is a line in the code
from some_module_name import *
This could easily explain why you can call these functions (classes?). It will also tell you what file to look in to modify the code for LogTrack.
Edit:
So, a little digging seems to find that LogTrack is part of PyEPL's textlog module. These other classes are from other modules. Somewhere in this person's code should be a line something like:
from PyEPL.display import VideoTrack
from PyEPL.sound import AudioTrack
from PyEPL.textlog import LogTrack
...
This means that these are classes specific to PyEPL. There are a few ways you could go about modifying how they work. You can modify the source of the LogTrack class so that it operates differently. Perhaps easier would be to simply subclass LogTrack and change some of its methods.
Either of these will require a fairly thorough understanding of how this class operates.
In any case, I would download the source from here, open up the code/textlog.py file, and start reading how LogTrack works.
I have an executable (converted to exe from python using py2exe) that outputs lists of numbers that could be from 0-50K lines long or a little bit more.
While developing, I just saved them to a TXT file using simple f.write.
The person wants to print this output on paper! (don't ask why lol)
So, I'm wondering if I can output it to something like HTML? XML? Something that could display tables of 50K lines and maybe 3 columns and that would also run in any PC without additional programs?
Suggestions?
EDIT:
Regarding CSV:
In most situations the best way in my opinion would be to make a CSV. I'm not opposing it in anyway, rather I think others might find Lott's answer useful for their cases. Sorry I didn't explain it that well in my question as far as my constraints go.
My constraints are: the user doesn't have an office suite, no python installed. Just think of a PC that has the bare minimum after a clean windows xp/vista installation, maybe Internet Explorer 7 or 8. This PC has to be able to open my output file and allow for reasonable viewing, searching, and printing.
CSV.
http://docs.python.org/library/csv.html
http://en.wikipedia.org/wiki/Comma-separated_values
They can load a spreadsheet and print anything they want.
If you can't install anything on the computer, the you might be best off outputting an HTML file with the data in a <table> that the user could view/search/print in IE.
You could use LaTeX to produce a PDF, maybe? But why exactly isn't a text file good enough?
You can produce a PDF using Reportlab. After all if you really want full control of the printed output, there's nothing that beats PDF.
Does 50k lines make too large a file? If not, just continue writing text files. Otherwise an easy solution would be to continue spitting out text files and compress them, e.g. with zip. You could use the zipfile library in Python. Most computers have no trouble reading zip files.