私は次のコードを使用して、特定のディレクトリのすべてのフォルダをユーザが選択できるリストに入れたいとします。下に私は解決策がありますが、私の懸念は、stdout情報が変更される可能性があり、stdin情報を無視するようにリストをハードコードしたくないということです。Python:stdoutのstdin行を取り除く方法
以下の行はlineList = lineList[6:-3]
です。
sshCommand = "plink [email protected] PASSWORD"
lsCommand = "ls -1 --color=never -d */\nexit\n"
sshProcess = Popen(sshCommand,shell=False,stdin=PIPE,stdout=PIPE)
sshProcess.stdin.write("cd /mnt/PCAPS/GroupSetup\n")
sshProcess.stdin.write(lsCommand)
sshProcess.stdin.flush()
lineList = []
for line in sshProcess.stdout.readlines():
line = line.replace("/","")
line = line.strip('\n')
line = line.strip('\r')
lineList.append(str(line))
lineList = lineList[6:-3]
while(True):
x = 0
for line in lineList:
x+=1
print "(" + str(x) + ") " + line
network_name = raw_input("Please select which network to use: ")
try:
network_name = lineList[int(network_name)-1]
networkCorrect = raw_input('You have selected %s. Is this correct? (Y/N): ' %(network_name))
if inputYNChecker(networkCorrect):
return network_name
else:
print "\nPlease select a number listed above.\n"
except:
print "\nPlease select a number listed above.\n"
私には次のような結果が得られます。
Using username "root". (1) A (2) B (3) C (4) D (5) E (6) F
私は私のコードでlineList = lineList[6:-3]
を持っていないときしかし、私が取得:
Using username "root". (1) Last login: Thu Dec 8 13:59:51 2016 from 255.255.255.255 (2) cd mntPCAPSGroupSetup (3) ls -1 --color=never -d * (4) exit (5) ←]0;[email protected]_Control:~[[email protected]_Control ~]# cd mntPCAPSGroupSetup (6) ←]0;[email protected]_Control:mntPCAPSGroupSetup[[email protected]_Control GroupSetup]# ls -1 --color=never -d * (7) A (8) B (9) C (10) D (11) E (12) F (13) ←]0;[email protected]_Control:mntPCAPSGroupSetup[[email protected]_Control GroupSetup]# exit (14) logout (15) ←[H←[2J
は、標準出力の「ゴミ」の復帰を取り除くために良い方法はあります特定の行を無視する以外に?
あなたのコードは、リモートの場所でフォルダやファイルを見つけようとしているか、あなたのコードですあなたがアクセスしようとしているフォルダーとファイルと同じマシン上にあります。 – MooingRawr
リモートサーバーに接続し、特定のディレクトリに移動し、そのディレクトリ内のすべてのフォルダを一覧表示します。上記の例でIPとフォルダの名前を変更しました。 – Kevin
行を印刷するかどうかを決定する正規表現またはその他のフィルタ関数を作成することがあります。 – sytech