top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python shell: Arrow keys not working in PuTTY

+2 votes
450 views

I want to use the Python 3.4 interpreter interactively (with centos 5), via a PuTTY ssh session. Currently, the arrow keys do not work:

$ /usr/local/bin/python3.4 

Python 3.4.2 (default, Feb 11 2015, 15:06:33) 

[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux 

Type "help", "copyright", "credits" or "license" for more information. 

>>> ^[[A 

I tried the following thread also
http://stackoverflow.com/questions/893053/python-shell-arrow-keys-do-not-work-on-remote-machine

which suggests that the problem can be fixed by installing the readline package followed by the rebuild of Python. Can anyone comment on the easiest way to fix this? Is a rebuild of Python necessary?

posted Feb 23, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Please try below steps:

  1. Open a putty terminal to a linux PC (I'm using RedHat)
  2. Open Python 2.7.* - using the command python2.7 (note: error does not appear in 2.6)
  3. import codecs
  4. Now use the arrow keys, and these bizarre characters appear.
answer Feb 25, 2015 by Amit Kumar Pandey
Similar Questions
+1 vote
def town():
 print ("You stand in the middle of Coffeington while you descide what"
 " to do next, you have herd rumor of the Coffeington Caves that run"
 "under the city, would you like to check them out?")
 answer = input()
 if answer == ("yes") or ("Yes") or ("y"):
 print("You set out for the Coffeington Caves")
 elif answer == ("no") or ("No") or ("n"):
 print("Oh...well im sure you can find something else to do")
 else:
 print("You just stand there")
town()

I don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end?

+1 vote

Here is something I found curious about python loops.

This loop run each character in a string

def avoids(word,letters):
 flag = True
 for letter in letters:
 if(letter in word):
 flag = False
 return flag

The loop below (at the bottom) runs each line of the file

fin = open('wordplay.txt');
user_input = raw_input('Enter some characters: ')
count = 0
for line in fin:
 word = line.strip()
 if(avoids(word, user_input)):
 count += 1;

This is just too convenient. Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Any comment is greatly appreciate.

+4 votes

I have an application which sets up logging after parsing the args in the main() funktion. It needs to be setup after parsing the args because I can set the loglevel via commandline flags.

I have tried many variants on how to do that but every time with an weird result. What I want is logging in from all libs and really understand that doing this should be enough there:

from logging import getLogger

logger = getLogger(__name__)

But, I need to setup the logger in the main() function to log only to a file and not to console because my application has an own shell interface which should not be spammed with log messages - never a message should show up there.

I think it should be only some few lines of code but I can't figure that out. The logger should be configured to have a max file size and rotate logfiles. Can someone help me with this?

+1 vote

Let's say I want to compare two csv files: file A and file B. They are both similarly built - the first column has product IDs (one product per row) and the columns provide some stats about the products such as sales in # and $.

I want to compare these files - see which product IDs appear in the first column of file A and not in B, and which in B and not A.
Finally, it would be very great if the result could be written into two new CSV files - one product ID per row in the first column. (no other data in the other columns needed)

This is the script I tried:

import csv

#open CSV's and read first column with product IDs into variables pointing to lists
A = [line.split(',')[0] for line in open('Afile.csv')]
B = [line.split(',')[0] for line in open('Bfile.csv')]

#create variables pointing to lists with unique product IDs in A and B respectively 
inAnotB = list(set(A)-set(B))
inBnotA = list(set(B)-set(A))

print inAnotB
print inBnotA

c = csv.writer(open("inAnotB.csv", "wb"))
c.writerow([inAnotB])

d = csv.writer(open("inBnotA.csv", "wb"))
d.writerow([inBnotA])

print "done!" 

But it doesn't produce the required results.
It prints IDs in this format:

247158132n

and nothing to the csv files.

...