ConfigParser won't save changes made to values in .ini file - python

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! :)

Related

Can't write to file, even though I have permissions to do so In Python

As the tittle implies, i am having trouble writing to a certain file, even though I have permissions to do so. I'm not getting an error; It's like the lines of code are just being ignored. They're not doing anything.
f = open("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/StartUp/startupfile.py", 'w')
f.write("Stuff I want to write")
f.close()
Can anyone help me? Again, there's no error message. The lines are just being flat-out ignored. And yes, I am running it with permissions through the console. Is the problem that it's in the windows directory? Or am I just missing something painfully obvious here?
EDIT:
Thanks for the help everyone, but it turns out that the problem was just Windows being incompetent. I found that the files appeared after a restart, which works anyway because I'm writing in the Startup folder.
I don't have enough reputation to make a simple comment, so don't interpret this as a solution to your problem, but did you try opening the file in the "wt" mode instead of only "w"? "w" mode opens the file in binary as seen here, so the issue could be that the program doesn't understand what you are trying to say because you want to edit it in the text mode. Again, I'm not sure if this is right and I'm a newbie myself, so don't interpret this as a solution, more like a commentary.

python create temporary file, what is the interest?

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.

How to know if excel file has macros using python?

HI I am new managing excel files, I would like to know if there is any variable in xlutils, xlrd, xlm, etc libraries that give true or false in case Macros are activated or now? Is there any way to know it from metadata before open? Wat is the best approach for know it?
Thanks a lot in advance.
It's technically possible but very unlikely that Excel is calling any Python functions.
A quick Google Search shows lots of people asking if VBA can use Python, and a few theoretical responses, but nothing solid.
What I would do to confirm, is move all the "mystery python scripts" to another folder. If someone complains that things suddenly don't work any more, then move them back.
If a couple months pass and everything's still working properly, it's unlikely that anyone needs them.
Edit:
Figures... as I was writing that answer someone posted a VBA/Python question!
...but out of curiousity, I queried SE Data Explorer:
Questions with tags like %vba% or %excel%: 212,299
Questions with tags like %python% and (%vba% or %excel%): 5,808 (2.7%)

How to open a Unix Executable File using Python?

Sorry if this might be an easy question, but I'm trying to open a Unix Executable File using Python, but it doesn't have any file extensions attached to it. The file name looks something like 'filename_bib'. I typed this and it worked:
hdulist = open('filename_bib')
But next when I typed in hdulist.info() or hdulist.shape(), it doesn't give me anything, so I checked all its attributes and tried print(type()) and hdulist.attribute? for each attribute, but I didn't really understand any of the explanations, so I actually tried typing all of them to see what they would give me, but at some point it started giving me errors which said:
ValueError: I/O operation on closed file
so I think this may have happened when I tried using hdulist.close() or hdulist.closed(), but I don't know (1) if it was a mistake for me to try any of the attributes, (2) if it somehow changed anything from my original file, and (3) how to fix it.
I was told that this file contains bytes and that I should somehow be able to show a picture from it using Python, but this is my first time handling Unix Executable Files, and I have absolutely no idea how to start. I've handled fits and pl files before, but this is my first time trying to open something like this. I've tried looking up a bunch of things online already, but I can't find any instructions whatsoever. Please help me out if you know anything about this. I will be very grateful for any help that you could give me.
This is what it shows when I open it in Sublime:
enter image description here
As the default file access mode in python is "read only". Technically, since you have not mentioned any access mode in your command
hdulist = open('filename_bib')
file should only be for reading and nothing should have happend to the opened file.
Question:
Have you tried running it in UNIX by,
./filename_bib
What was the output?

Help with PyEPL logging

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.

Categories

Resources