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
Related
I can get python to open an excel file and then save that same excel file, but that’s causing issues when I merge cells and then try to run the program again. I could resolve this issue if I can open the same excel template via python every time and then when the program is finished, it will save that excel file as a user defined name within the same folder the template was opened from. I know there is a saveAs function but I can’t find enough information to be able to get it to work. Thank you!
This is my existing code:
input = open("first file.txt", "rt")
output = open("revised.txt", "wt")
for line in input:
output.write(line.replace("Bob", "James").replace("Xavier", "Jake"))
input.close()
output.close()
Now, this works but I have to manually add "first file.txt" to python and the program doesn't search the computers files. How would I make the program automatically search the computers files? Attached is a photo of where I have to drag in case your confused.
You're not "adding files to Python". You're adding them to REPL.it's website, which have their own file-system, and is where the code reads the files from.
Since you are using an online web interface, there is no possible way to make it search your local computer's files.
If this is what you want, install Python and run your code locally.
The script needs to read from a .txt file with a list of items and based off that list, it creates a directory for each item. So just a simple with open() statement and then os.mkdir()..
The client will add account names to the txt file when they get new accounts, the program will create new directories for those new accounts. Files that are then placed in each directory for those accounts will get pandas stuff done to them and moved to an archive folder along with logging the events.
Since the program will be used by non-programmers, I'm using Pyinstaller.
When I go through the process, including the --add-file attribute, run the program, it says the txt file doesn't exist. I think I have an idea how to fix that but when I got the os.mkdir and with open statements to work by separating them out, it created them in the home folder and not the program folder. Path issue? And I only see documentation on how to include data files but not how to include changeable files.
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.
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