2017-01-13 11 views
1

に呼び出されたときにファイルに私はサブプロセスを開き、ファイルへの出力にそれを伝えたときにそれが動作する理由誰も私に説明することができますしないでください。Pythonのサブプロセス出力ループ

currentSharePath = '\\\Path1' 
p = subprocess.Popen([r'powershell.exe', 
       '-ExecutionPolicy', 
       'Bypass', 
       os.path.dirname(os.path.abspath(__file__))+'\subFoldersClassifier.ps1', 
       '-sharePath', 
       currentSharePath, 
       '2>$null', 
       '| Out-File -encoding utf8 -append classified_subFolders.txt'], 
       stdout=sys.stdout) 
p.communicate() 

しかし、ときに私が呼んでそれは、ループ内での出力は、ファイルの代わりに私の端末に示されている:

with open('found_shares_cleaned.txt') as f: 
    content = f.readlines() 


for currentSharePath in content: 
    p = subprocess.Popen([r'powershell.exe', 
        '-ExecutionPolicy', 
        'Bypass', 
        os.path.dirname(os.path.abspath(__file__))+'\subFoldersClassifier.ps1', 
        '-sharePath', 
        currentSharePath, 
        '2>$null', 
        '| Out-File -encoding utf8 -append classified_subFolders.txt'], 
        stdout=sys.stdout) 
    p.communicate() 

は、事前にありがとう

答えて

0

問題は、それが改行CHを挿入readlines()方法ループですされていません各ラインの終わりのアトラクター。

ので、あなたの引数の一つは(何もしない、次の行で行わリダイレクトをし、スキップ、)コマンドラインを終了するのに十分であり、改行、

可能な修正、strip末尾に改行が含まれています。

for currentSharePath in content: 
    currentSharePath = currentSharePath.rstrip() 

またはこのような行のリストを生成:

with open('found_shares_cleaned.txt') as f: 
    content = [line.rstrip() for line in f] 
+0

はありがとうを!それは完璧に動作します –

+0

受け入れていただきありがとうございます。もう別のupvoteの価値がある:) –

関連する問題