How would I get Python to create a file [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What I'm trying to do is get Python to create a batch file for me which I can put what the text is that I need in the file just by coding it. What I began to do is create a text file with the open() method, I then added the text I need in the batch file using write() but now I want it to be a .bat file with the text in that text file, any ideas?

When you create a new file with the open() function, you state its file name. Just give it a name with the .bat extension, and it has become a .bat file. Of course, this assumes the program is running in the Windows environment, where the concept of a .bat file makes sense.
Then just write the appropriate text into the file, then close the file properly with the close() function, or even better using the end of the with construct. You may need to flush the file system to ensure that all text was written to the file.
If you need more details, add more details to your question about your specific difficulty.

Related

how to inject code with a batch into a batch that has existing code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
do I convert it a txt file? how do I inject the new line in between the other lines? I'm trying to inject a wallet address to a simple mining batch file without needing to physically open it prior.
pretty much the last step to automating my mining rigs for full self sufficiency.
if anyone has any way of doing this, please describe in full detail or show an example, as I am self taught and in way over my head for a project that's exceeding expectations before release lol
One option is to use a settings file, and if that file doesn't exist, say on the first run, to create it instead:
#echo off
setlocal enabledelayedexpansion
if not exist example_settings.cmd (
echo Creating settings file
rem Prepare a helper batch file to store the settings
echo|set /p="set _key=">example_settings.cmd
rem Imagine this "echo Example" is instead a command that returns a new value
echo Example>>example_settings.cmd
)
call example_settings.cmd
echo The setting is %_key%
This will create a settings file with the key set, and on subsequent runs, use that key from the helper batch file instead of trying to create a new one.
I would read in the entire file with f.readlines() so you get a list of strings (where each string represents a line in the file), write some logic that determines where the new string should go in between, and then re-write that to a file after.

Dashboard for monitoring the results of an iterative program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I run a Python code in which an iterative process is done. Every few minutes an iteration is performed and the results are stored in a file. Currently, after each iteration I have to run another Python script to plot the recent results to monitor the progress. I want to have a dashboard which plots the recent results whenever the results file is updated.
It's not entirely clear what your question is, but it sounds like you want to monitor the output file for changes and plot them when the file is changed.
If you're using Linux (as the tag suggests), then I'd suggest using inotify, which is a Linux API allows you to monitor filesystem events (like file writes!).
There is a Python wrapper around this, also named inotify: https://pypi.org/project/inotify/. You should be able to add a watch on your log file and run your plotting function when it's modified (perhaps by watching for the IN_CLOSE_WRITE event).

Edit/view the command functions referenced by the key binding JSON script in Sublime Text 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here's a custom key binding I have in ST3:
{ "keys": ["super+shift+e"], "command": "open_dir",
"args": {"dir": "$file_path", "file": "$file_name"} }
I would like to edit/view the command functions referenced in this JSON key binding (e.g. open_dir, which opens Finder). I want to see the code behind open_dir to see how it works.
The JSON file of this key binding is Default (OSX).sublime-keymap. I've done file searches in ~/Library/Application Support/Sublime Text 3/ as well as in Applications/Sublime Text/ for command names such as open_dir but I still can't find the code of the commands that the JSON script is referencing.
Is there a way to find and view the commands that the JSON script is referencing?
EDIT: Thanks and apologies to those who commented before I made this edit. I've changed the title and the question text pretty heavily to try to be clearer since the post got put on hold.
JSON is parsed, not interpreted. Plus they are not executable, much like a plain text file.
Therefore, you use python as normal to run a subprocess, but from a parsed value rather than a hard coded command
Its a bit unclear from your question what your end goal really is. You should provide some examples.
If you are looking to parse JSON data, extracted from a file, and execute in the terminal, then using the subprocess is likely the way to go.
Calling an external command in Python

Display a file to the user with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find a way to display a txt/csv file to the user of my Python script. Everytime I search how to do it, I keep finding information on how to open/read/write etc ... But I just want to display the file to the user.
Thank you in advance for your help.
if you want the file to open with its associated default program, use startfile.
os.startfile("path/to/file") # may only work on Windows
It really depends what you mean by "display" the file. When we display text, we need to take the file, get all of its text, and put it onto the screen. One possible display would be to read every line and print them. There are certainly others. You're going to have to open the file and read the lines in order to display it, though, unless you make a shell command to something like vim file.txt.

Is it possible to read Word files (.doc/.docx) in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to create a validation tool;
Can any one help me read .doc/.docx documents in Python in order to search and compare the file contents.
Yes it is possible. LibreOffice (at least) has a command line option to convert files that works a treat. Use that to convert the file to text. Then load the text file into Python as per routine manoeuvres.
This worked for me on LibreOffice 4.2 / Linux:
soffice --headless --convert-to txt:Text /path_to/document_to_convert.doc
I've tried a few methods (including odt2txt, antiword, zipfile, lpod, uno). The above soffice command was the first that worked simply and without error. This question on using filters with soffice on ask.libreoffice.org helped me.
You can try using PyWin32 to access Word via COM, although that will be a little ugly. You could also look at IronPython since it's built with .NET and may have better hooks into Office.
See also the following:
Reading/Writing MS Word files in Python
https://github.com/mikemaccana/python-docx
http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/
http://www.galalaly.me/index.php/2011/09/use-python-to-parse-microsoft-word-documents-using-pywin32-library/

Categories

Resources