In Python3, using nano, the very first line I create somehow gets "tabbed" over about several characters. This does not happen when the source code is initially created. E.g. the program can contain only one line, a simple:
print ("Hello World")
Once you save and you re-enter the program, it looks like this (moved to the right several spaces)on the first line:
print ("Hello World")
A 'cat' on the program does not show the first line "tabbing". The line is left justified (i.e., it looks normal). The program runs fine and without errors. It is important to note that re-entering the source in nano OR vi subsequently makes that first line totally disabled and it cannot be edited. What is possibly causing this?
Related
I am using a script to connect to another server. The skeleton looks something like this:
import pexpect
...
child = pexpect.spawn(f"""ssh {username}#{host}""")
child.setwinsize(136,1000)
child.expect("Password:")
child.sendline(pwd)
However, when I enter a very long input it wraps around to the next line. The problem I am facing is that when I try to erase it (backspace-it), it doesn't take me to the previous line. So the input stays as is on the previous line. But this is how it is shown to me in the terminal, behind the scene the input gets backspaced (if that's a word).
I am using Python 2.7 and the function, f.write() is not working. A part of my code is given below.
Please suggest if any packages need to be installed.
for item in data1['OperationalTimes']['airportResources']:
with open("airportResources_details.txt",'a') as f: -- code works fine till here when i try to "print data1"
f.write(item['arrivalTerminal']+'\n') -- this line is not getting through
For a start, you can avoid the performance drain of opening and closing the file for every single item, by simply changing the order of things:
with open("airportResources_details.txt",'a') as f:
for item in data1['OperationalTimes']['airportResources']:
f.write(item['arrivalTerminal']+'\n')
Beyond that, you may want to check the usual suspects, like ensuring you're running in the correct directory. For example, use os.system("pwd") for getting the current working directory (on a UNIX-like platform).
Or temporarily changing the file specifier to something like /tmp/xyzzy.txt and seeing if that gets created in the right place.
Or temporarily changing it to use print rather than f.write to see if it comes out on standard output.
I'm having a strange problem where some Python code that prints to a file is not inserting an EOF character. Basically, the Python script generates runscripts to later be submitted as jobs on a cluster. I essentially wrote the entire runscript between """'s, allowing for variables to be plugged in (to vary some parameters in my simulation). I write the runscripts using the
with open(file_name, 'w') as runscrpt:
runscrpt.write("""ENTIRE_FILE_CONTENTS_HERE""")
syntax. I can give the actual code if necessary but it's not much more than above. Despite the script running fine and generating all of my runsripts, whenever I submitted them nothing happened. It took me a long time to figure out why, but it's because they're missing an EOF character. I can fix it by, for example, opening one, adding some trailing whitespace or a blank line somewhere in vim, and resaving the file.
Why isn't Python inserting the EOF character, and is there a better way to fix this than manually making trivial edits to all the files with vim?
Sounds like you mean there is no EOL (not EOF!) at the end, because that's what diff will typically tell you. Just add a newline at the end of the write (make sure there is a newline before the final """ terminator, or write a separate newline explicitly).
with open(file_name, 'w') as runscript:
runscript.write("""ENTIRE_FILE_CONTENTS_HERE\n""")
(As a bonus, I added the missing vowel.)
I'm trying to learn how to use pdb in emacs.
I run emacs in console mode and in my python file I have something like import pdb and pdb.set_trace() at the beginning of the file. I use C-c C-c to execute the buffer and pdb starts running. It works fine except that I end up with a => inserted into my code on the line that pdb is looking at. When pdb ends, the => characters remain in my code on the last line and I have to manually delete it. How do I prevent this from happening?
Normally there is no "=>" inserted at all. What there is instead is a "=>" that is displayed but which is not part of the buffer's content. Are you sure it's really in the code and are you sure you can delete it as if it were normal text?
I have a super simple code and when run the first time it does not write to the file. But when run a second/multiple times later it writes to the file. The same thing happens when using "w" instead of "a" as well.
It also seems that the file is not closed after fh.close is run because I am unable to delete it - and a message appears saying that python is using the file. Any suggestions? Thanks!
fh = open("hello.txt","a")
fh.write("hello world again")
fh.close
fh.close doesn't call close, it just refers to the function. You need to do fh.close() to call the function.
you need to put the brackets after fh.close else you aren't actually calling the function, and if you are running interactively (i.e. with IDLE) then the interpreter keeps the file open.
so change your last line to:
fh.close()
James
The other posters are correct.
Also, I would suggest using the "with" statement when working with files, because then they will be automatically closed when your code goes out of scope.
with open("hello.txt","a") as fh:
fh.write("hello world again")
# Code that doesnt use the file continues here
If you use this, you never have to worry about closing your file. Even if runtime errors occur, the file will still always be closed.