Python: permission to change file only through the script - python

I wrote an application that creates csv file using with open and changes them according to users choises.
I want restrict the files so the users won't be able to open the files outside the software (on excel or any other program) and change them (content and file characteristics) through those softwares, without restricting the ability to change the files through my software.
thanks,
Lior

You can't really do this with a .csv because the operating system controls that sort of access. You can make the file read-only, and have your program change the read-only flag before processing it. But your user will be able to defeat that. Realistically, you have three options:
You can encrypt the file so that only your code understands it.
You can calculate a hash of the file and store it. When your code opens the file it can check the hash and refuse to process it if the file has been changed.
You can switch to storing your data in a database. Databases have the sort of access control that you want. For example, your program can have a username/password combination that only it knows.

Related

Sharing Python script that reads a .txt file on my computer

I have created a python script that works really well. It opens a .txt file and picks a word a random before using that for a quiz. If I want to share this code do I need to share the .txt file also and does the receiver have to change the path to the .txt file in the code to the location of the txt file on their computer?
Other than putting the words as a list directly in the code is there a way of referencing the .txt file to the code in such a way that it will work on any computer?
You can change the hardcoded path and replacing it using a variable that will reference the file path to your text file and ask for the input when the scripts, in various ways,
use input() to ask for user input
read as an CLI argument that user triggers the script
maintain a user configuration file and have the script read it
...
whatever ways you can think of that is efficient and suitable for your use case

Data storage for standalone python application

I want to make a python program (with a PyQt GUI, but I don't know whether that is relevant) that has to save some information that I want to store even when the program closes. Example for information I want to store:
The user can search for a file in a file dialog window. I want to start the file dialog window in the previously used directory, even if the program is closed in between file searches.
The user can enter their own categories to sort items, building up on some of my predefined categories. These new categories should be available the next time the program starts.
Now I'm wondering what the proper way to store such information is. Should I use pickle? A proper database (I know a tiny bit of sqlite3, but would have to read up on that)? A simple text file that I parse myself? One thing for data like in example 1., another for data like in example 2.?
Also, whatever way to store it I use, where would I put that file?
I'm asking in the context that I might want to later make my program available to others as a standalone application (using py2app, py2exe or PyInstaller).
Right now I'm just saving a pickle file in the directory that my .py file is in, like this answer reconmends, but the answer also specifically mentions:
for a personal project it might be enough.
(emphasis mine)
Is using pickle also the "proper, professional" way, if I want to make the program available to other people as a standalone application?
Choice depends on your approach to data you store, which is yours?:
user should be able to alter it without usage of my program
user should be prevented from altering it with program other than my program
If first you might consider deploying JSON open-standard file format, for which Python has ready library called json. In effect you get text (which you can save to file) which is human-readable and can be edited in text editor. Also there exist JSON file viewers and editors which made viewing/editing of JSON files easier.
I think SQLite3 is the better solution in this case as Moldovan commented.
There is a problem in pickle, sometimes pickling format can be change across python versions and there are greater advantages of using sqlite3.

How to get around Heroku resetting files?

I have to python files that create and read text from a .txt file, in order for them to work they need to know the info inside of the .txt file.
In heroku I have a scheduler that runs one file, then the other. The big problem is that the files are reset every time to their state from the original repo. How can I get around this?
Heroku does not offer a persistent file system. You will need to store them in another service (like S3), or depending on what the contents of your files are, redesign to write and read from a database instead.

Changing the content of the file in python

Is it possible to have a script (written in Python) that can change a content of a certain text file according to a database records.
For example: I have a DB that has some records of different types and a script that retrieves a single record from the database and according to that record it automatically changes a content of the text file that is somewhere on the system (e.g. tempo_file.txt).
I know that Python can read from database, read from file and input to the file. But what kind of logic should I apply to overcome this problem? Thanks.
You have to realize, when read from I file, you actually read it to the memory. You can manipulate it in your program.
Then you can flush the bytes in your memory into the disk. AKA save it or write it. If you want to "automatically change something", I guess you have to just read from the file and write to the file that has the same name.
There really is no way to manipulate it on the hard disk.
what kind of logic should I apply to overcome this problem?
Well quite simply:
read from the database
open the text file
write to the text file
close the text file
lather, rinse, repeat

is it possible for multiple users to write data into a file (excel or text) using python

I am trying to get some info (which keeps changing) from different computers into a text file. Is it possible to write using python file by creating a text or excel file.
This is how I am planning
1. The file sits on a common location accessible by all the computers.
2. The python script runs in each computer and updates the file in the common location
3. Whenever I open, I should see the information that is updated.
Problems will arise when opening and saving files by multiple computers.
What happens when a computer opens a file, and saves data on top of another save?
The situation you described would be well suited to a database. A popular open-source and free database server is MySQL which can handle multiple connections and users reading and modifying the data using a language called SQL, and yes, python can connect to databases with this package.
And of course, you can export the database to a spreadsheet file to review on your computer.

Categories

Resources