How do I close a file in the terminal - python-3.x

I've only started doing python3 for a paper at university. I don't plan on continuing after this semester is over but I want to do the best I can in this class. However, I am having difficulties with seemingly a basic task.
I've opened a file using f = open() and I've accessed it in the terminal using less. It displays everything out nicely but it doesn't let me close the file or even continuing to code past the printed text.
It just repeats ~ on separate lines and finishes with a highlighted (END)

By typing q, you will be able to exit it

Related

Python file immediately closes

As an assignment for one of my classes, the professor had us download a file containing python code as well as a couple of text files that the python code will analyze.
The problem is, when I click to open the python file, the command window opens, then immediately closes. I can get it to stop from closing if I click on it fast enough, but that stops the code from loading, and any keystroke causes it to close again.
It's the same file every other student downloaded and used with no issue. I've tried re-downloading the file, re-installing python, even trying on a different computer. I've also tried opening it by entering the path file name in the command window with no success. Also no luck trying it in Jupyter Notebook or CodeLab. (And of course, no luck experimenting with the slew of possible solutions on previous Stack Overflow, Reddit, etc. questions).
Any ideas on the potential cause and/or solution?
Edit: cause unknown, but the solution was opening the file with Sypder console.
the file closes immediately because It ended and returned 0, to stop that from happening you can add at the end of the python code something like that:
ending = input("press key to exit")
or just import time and use
time.sleep(5)
you can open your file in the cmd/terminal window and you will be able to see the results

How do I avoid losing program focus after subprocess.call?

I am trying to extract data from a program; its raw data files are encrypted XMLs so my plan is to start the program, then use mouse control commands (e.g. pyautogui) to copy / paste the data.
So far I'm falling at the first hurdle; when I run subprocess.call to start the program, the program gets window focus and my subsequent code doesn't execute until I manually close the program.
workingFolder = self.get_working_folder() # get path where raw data is
subprocess.call('Path\Program.exe') # works, Program.exe starts
print(workingFolder) # this doesn't execute until I close the Program window
EDIT: After further testing I have answered my own question - subprocess.Popen was in fact what I needed, despite my initial doubts about it! Thanks to anyone who looked at my question :).
workingFolder = self.get_working_folder()
subprocess.Popen('Path\Program.exe')
print('This works')

Is it possible to run Python code in Sublime Text3 as I code along?

I think I had seen a video somewhere on YouTube earlier which showed someone coding Python 3 on Sublime Text3. It worked as follows:
He types:
x=10
y=x+5
And automatically both of these statements get enclosed in a block and an output gets printed in the console as follows:
x=10
y=15
Then he continues coding another line which gets enclosed in a separate block.
z=x+y
And the execution seems to continue the state from last time because the output shows:
z=25
These weren't the exact statements but I'm trying to communicate the idea over here. I cannot find that video or any information related to this after a lot of searching. Perhaps I'm using wrong keywords.
Is there a way to run Python3 code in Sublime Text3 in real time (perhaps they're using a built-in terminal that retains state while coding a single program?).

Control - Z In Python Code

In IDLE, there's no clear screen, and after reading the forums the best method is plainly to print a ton of "\n"s. However, after playing with IDLE, I made a discovery. When my game was waiting for an input after printing out instructions using print statements, instead of inputting any useful character, I entered 'control-z,' and IDLE began to remove the text that was display by the print statements, one by one.
My main question is, how do I manually in the code itself enter 'control-z', so I can utilize this functionality?
If you're confused by my story, here's some example code and try it yourself by hitting control-z.
print("Some Text")
print("More Text")
input("The Input (Hit Control - Z Here):")
^Z is a bit of a mess. The ascii control char ^D is usually interpreted as EOT, end of transmission, which on Unix and many other systems means end of file, close the application. Ascii ^Z is meant to be interpreted as SUB, substitute, whatever that means. Editors ofter use it as UNDO (meaning undo a ^X cut). Microsoft (and a few other old systems) at least sometimes interprets ^Z as end of file, with the same effect as ^D on *nix.
The Windows console closes a text app after ^Z . ^D is passes on to the app. IDLE, as a cross-platform app, closes on ^D. IDLE used to close on ^Z on Windows, but now, for me, it only erases the prompt. (I don't know if this alternative is intended.) I do not see the progressive deletion you report. What OS and what Python version are you running?
To answer your main question: you can't. input is usually used in assignment statements: string = input('prompt'). The way to imitate input statements is to directly assign 'user input': s = 'simulated user input'. However, this does not work for characters that get intercepted by the programs managing the input window and never sent to the python program.
IDLE's Shell generally imitates Python's interactive console. The latter (at least on Windows) makes everything, except the current input, read-only. Shell follows suite. Imitation is especially strict as regards executing user code. It is intended that user code tested in IDLE should run in Python without IDLE. It would be wrong for IDLE to clear the interactive shell in response to user code when Python cannot.
For editor and output windows, ^A (select all) followed by Backspace (delete), Delete, or ^X (cut) do clear the window.
Shell does, however, has more editing commands than many (most? all?) consoles and a menu system. These additions are allowed since they are interactive only and not accessible from user code. There have been various proposals and patches to enable clearing part or all of the shell window. https://bugs.python.org/issue6143 has some of the discussions and proposals.

Matlab command window tabs (tabbed)

Hi I hope this question conforms to community guidelines. While working in Matlab I'd really appreciate if the command window had tabs (much like most terminal emulators in Linux). Is available in Matlab or do I have to run different instances of Matlab?
I am running a system which produces plots so running Matlab in text based mode via -nojvm is not possible.
EDIT: Is there a way to get such a feature or do I have to wait for Mathworks to wake up and implement this simple, timesaving tool.
Hmm I think the way Matlab works (with the workspace and the editor) tabs might not be that beneficial in terms of performance as opposed to just running a new instance of Matlab.
In Linux, you can fork new MATLAB instances with unix command and pass the MATLAB command you want to run as a command line argument. If you add & to the end of the unix input string, the new MATLAB instance becomes a background process, so this way from one MATLAB command window you can run commands (with output) in several MATLAB instances, opening a new MATLAB instance for command execution and output.
Here's the code:
multithread.m:
function multithread(MatlabCommand)
% this is a function to create a new MATLAB instance and run a command in it.
unix([ 'matlab -desktop -r ', MatlabCommand, ' &' ]);
return
testfunction.m:
function testfunction()
fprintf('one two three.\n');
return
Then you can run MATLAB commands in a new instance this way: multithread('testfunction');.
As far as I know, there is no such feature.
Although are you aware that you can use the -nodesktop flag to run Matlab in the current console and still have the ability to bring up plots?
Well - depends on what you plan to do with the tabs. You do have tabs for file editor, variable editor. But I suppose you mean command window.
I sometimes have two or more instances of Matlab open - one doing some calculation and doing some observation and quick stuff from the other - but I don't find it advisable.
I have found against using multiple instances that the preferences and path get "unusable" regularly (say once every month).
But as Matlab is "single" threaded - while it's calculating it's near impossible to do simple tasks as editing code or open a file in the same instance. If it had tabs i imagine it even worse. I think this problems lie very deep inside how Matlab works so that will not change very soon.

Resources