I am making a hardcoded voice assistant and I want others to be able to use it as I do i.e. open applications and other stuff but the paths have my username in them so I am thinking I should create a txt file with all the voice commands (in python syntax) and make a script that copies the contents of txt file to a py file, so that others can copy paste and add more commands simply by editing the txt file.
How do I do it? Or is there a simpler way to do this?
with open("filetowrite.py","wb") as fout:
with open("filetoread.txt","rb") as fin:
fout.write(fin.read())
I guess ... seems like a strange method to use to me though
Related
I'm trying to have a program output data to a JSON file, but VS code or Python itself seems to have a problem with that. Specifically, I'm trying to output this(Tlist and Slist are lists of integers):
output = {"Time": Tlist, "Space": Slist}
json_data = json.dumps(output, indent=4)
with open("sortsOutput.json", "a") as outfile:
outfile.write(json_data)
But nothing seems to be happening. SortsOutput.json was never made, and even with a pre-existing SortsOuput.json nothing happened. Heck, this doesn't even work:
out = open("blah.txt", "w")
out.write("Egg")
out.close()
What might be going wrong for my software for this to happen? I'm using Python v2022.16.1, for the record, and every time the program runs for the first time the command "conda activate base" happens with some error text that doesn't seem to affect the rest of my program, so is it that? How do I fix that?
out = file.open("blah.txt", "w")
In your second example, it seems that you don't need file.. open() is a built-in method of Python.
You can use out = open("blah.txt", "w") directly.
At the same time, this problem seems to have nothing to do with vscode.
I think this problem is more likely due to the path problem. The .json file you executed does not exist in the first level directory under the workspace.
For example,
Workspace
-.vscode
-sortsOutput.json
-test.py
You need to use the workspace as the root directory to tell the specific location of python files:
with open(".vscode/sortsOutput.json", "a") as outfile:
outfile.write(json_data)
I was unable to write file using MS Visual Studio Community (python), in my case it was an encoding issue. I found the solution at: https://peps.python.org/pep-0263/ just put a special comment: # coding=<encoding name> at the first line of the python script
(in my case: # coding=utf-8)
I have a file which is not .txt extension, but I can right click and open it using notepad, and it's a readable file.
However, if I try to open the file I cannot retrieve the text, or edit it.
Here's some code to make things clearer:
path=r"C:\Users\Alon\Desktop\RUN Low.spe"
f=open(path,'r+')
f.readlines()
Output:
[]
Again, if I try to open this file using Notepad - no problems. I can read and edit the text, but I wanna do it via Python. Is there a solution to this?
I have the following problem:
I have a folder with files.
I want to write into those files their respective file path + filename (home/text.txt) .
How can I achieve this in python?
Thanks in advance for your time and help!
with open('FOLDER_NAME/FILE_NAME','w+') as f:
Assuming the python file is in the same path as the folder.
You can use:
..
to move back a directory.
file = open("path", "w+")
file.write("string you want to write in there")
file.close
With w+ it is for reading and writing to a file, existing data will be overwritten.
Of course, as Landon said, you can simply do this by using with, which will close the file for you after you are done writing to it:
with open("path") as file:
file.write("same string here")
This second snippet only takes up 2 lines, and it is the common way of opening a file.
However if you want append to instead of overwriting a file, use a+ this will open and allow you to read and append. Which means existing data will still be there, whatever you write will be added to the end. The file will also be created if it doesn’t exist.
Read more:
https://www.geeksforgeeks.org/reading-writing-text-files-python/
Correct way to write line to file?
I am working in the field of astronomy, and the process that I use to unzip the images that I get from the telescopes can be very tedious. The format that the images come in is 'fits.fz' which stands for fits.fits-zipped. I want to decompress these into just '.fits'. I have already I'm working on a program that simplifies this process of decompressing. I have created a graphical interface with two buttons through Python and Tkinter. The first button creates a text file named 'list.txt' and then executes a pre-existing .bat file which dumps the names of every file in a specific directory that ends with 'fits.fz' into 'list.txt'. The first button is also supposed to copy the specific names of the files into a very specific place in another bat file. The other .bat file is called 'Decompress.bat' and is supposed to use the following command for each file in 'list.txt':
C:\ds9\ds9.exe
C:\directory\FITS FILE HERE
-savefits
I would like for the python program to be able to copy specific sections from a line of code and paste them where 'FITS FILE HERE' is.
The following is the function that is executed when the first button is pressed.
f = open('C:/jah/list.txt')
f1 = open('C:/jah/decompress.bat', 'a')
def begin_wombocombo(): #Is function for first button
open('C:/jah/list.txt', 'w').close() #Clears 'list.txt'
open('C:/jah/decompress.bat', 'w').close() #Clears 'decompress.bat'
subprocess.call([r'C:/jah/newbat.bat']) #Dumps directory into 'list.txt'
doIHaveToCopyTheLine=False #Bool for whether or not the program has to copy line
for line in f.readlines(): #loops through all instances to find fz files and then pastes them into decompress.bat
if 'fits.fz' in line:
doIHaveToCopyTheLine=True
if doIHaveToCopyTheLine:
f1.write(line)
f1.close()
f.close()
The issue with this is that it only copies the lines of text that has the fits.fz files. This means that it copies everything else on the line such as when the file was created. Is there any way to simply copy and paste the fits.fz file alone? How would I go about working these strings into the .bat file?
Thank you for your time, and btw the second button just executes 'decompress.bat' which is the file with the commands to unzip the images.
I think in Python, something like this would do the trick, without writing out batch files etc.
import os
import subprocess
target_directory = 'C:\\directory\\' # change this as required
zipped_files = [x for x in os.listdir(target_directory)
if x.lower().endswith('.fits.fz')]
for filename in zipped_files:
subprocess.call([r'C:\ds9\ds9.exe', os.path.abspath(filename), '-savefits'])
Let me preface by saying I am very new to programming. I'm creating a fun program that I can use to start my day at work. One of the things I want it to do is display a random compliment. I made a text file that has multiple lines in it. How do I store that text file then open it?
I've opened text files before that were on my desktop but I want this one to be embedded in the code so when I compile the program I can take it to any computer.
I've googled a ton of different key words and keep finding the basics of opening and reading txt files but that's not exactly what I need.
Perhaps start with defining a default path to your file; this makes it easier to change the path when moving to another computer. Next, define a function in your program to read and return the contents of the file:
FILE_PATH = "my/path/to/file/"
def read_file(file_name):
with open(FILE_PATH + file_name) as f:
return f.read()
With that in place, you can use this function to read, modify, or display the file contents, for example to edit something from your file:
def edit_comments():
text = read_file("daily_comments.txt")
text = text.replace("foo", "foo2")
return text
There are obviously many ways to approach this task, this is just a simple example to get you started.