2016-10-26 7 views
1

私は、フォルダの内容を私のためにテキストファイルに出力することができる2つの引数をとるpythonスクリプトを書いています別のプロセスに使用する私が持っているのスニペットは以下の通りです:Python:フォルダの内容をテキストファイルに入れようとしています:

#!/usr/bin/python 
 

 
import cv2 
 
import numpy as np 
 
import random 
 
import sys 
 
import os 
 
import fileinput 
 

 
#Variables: 
 

 
img_path= str(sys.argv[1]) 
 
file_path = str(sys.argv[2]) 
 

 
print img_path 
 
print file_path 
 

 
cmd = 'find ' + img_path + '/*.png | sed -e "s/^/\"/g;s/$/\"/g" >' + file_path + '/desc.txt' 
 

 
print "command: ", cmd 
 

 
#Generate desc.txt file: 
 
os.system(cmd)

私は試してみて、私のコマンドラインから、私は次の出力を得ることを実行して、私はそれを修正する方法が分からないとき。

sh: 1: s/$//g: not found

は、私は新鮮なターミナルインスタンスで次のコマンドを実行して使用していますコマンドをテストし、それが正常にうまくいく:

images/*.png | sed -e "s/^/\"/g;s/$/\"/g" > desc.txt

私のスニペットがうまくいかない理由は誰にも見えますか?私はそれを実行すると、私は空のファイルを取得...

ありがとうございます!

+2

os.systemは問題が、代わりにsubprocess.callを使用してみています。それは引数を使って外部コマンドを呼び出す方法をはるかに大きく制御できます。 それでは、なぜシェルコマンドを起動するのですか? os.walkを使用してPythonですべて保持することはできませんか? –

+1

'fnmatch.filter()'や 'glob.glob( 'images/*。png')'と 'os.listdir()'を使うことができます。 – furas

答えて

0

Pythonが文字列の内容を処理しエスケープするため、正規表現のフルテキストをbashに送信しないため、最善の解決策は、文字列のバックスラッシュを手動でエスケープすることです。エスケープコードです。これに

cmd = 'find ' + img_path + '/*.png | sed -e "s/^/\"/g;s/$/\"/g" >' + file_path + '/desc.txt' 

cmd = 'find ' + img_path + '/*.png | sed -e "s/^/\\"/g;s/$/\\"/g" >' + file_path + '/desc.txt' 

、それはあなたのために働く必要があるので、この行を変更します。

あなたの質問にコメントが大きなポイントがあり、けれども、あなたは完全にだけ、のpythonからのようなもの、それを行うことができます:私は完全にカイルに同意

import os 
import sys  

def main(): 
    # variables 
    img_path= str(sys.argv[1]) 
    file_path = str(sys.argv[2]) 

    with open(file_path,'w') as f: 
     f.writelines(['{}\n'.format(line) for line in os.listdir(img_path) if line.endswith('*.png')]) 

if __name__ == "__main__": 
    main()   
0

を。私のお勧めは、あなたのコードからbashコマンドを呼び出すよりも、Pythonコードだけを使うことです。ここでは私の推奨コードですが、それは前述のものよりも長くて最適ではありませんが、IMHOは理解しやすいソリューションです。

#!/usr/bin/python 
 

 
import glob 
 
import sys 
 
import os 
 

 
# Taking arguments 
 
img_path = str(sys.argv[1]) 
 
file_path = str(sys.argv[2]) 
 
# lets put the target filename in a variable (it is better than hardcoding it) 
 
file_name = 'desc.txt' 
 
# folder_separator is used to define how your operating system separates folders (unix/and windows \) 
 
folder_separator = '\\' # Windows folders 
 
# folder_separator = '/' # Unix folders 
 

 
# better if you make sure that the target folder exists 
 
if not os.path.exists(file_path): 
 
    # if it does not exist, you create it 
 
    os.makedirs(file_path) 
 

 
# Create the target file (write mode). 
 
outfile = open(file_path + '/' + file_name, 'w') 
 

 
# loop over folder contents 
 
for fname in glob.iglob("%s/*" % img_path): 
 
    # for every file found you take only the name (assuming that structure is folder/file.ext) 
 
    file_name_in_imgPath = fname.split('\\')[1] 
 
    # we want to avoid to write 'folders' in the target file 
 
    if os.path.isfile(file_name_in_imgPath): 
 
     # write filename in the target file 
 
     outfile.write(str(file_name_in_imgPath) + '\n') 
 

 
outfile.close()

+0

はい、読みやすくなりますが、同じ目標。私は最も速い解決策を見せようとしていましたが、あなたのやり方は確かに無色です。 –

+1

ありがとう、カイル。私は代わりの、そして読みやすいソリューションを提供しようとしていました。あなたの解決策はずっと優れていますが、経験豊富な人のために) –

+0

ハハ、よく@DanielSerranoにもありがとう、時々私は誰もがピュータンズに精通しているのを忘れています。再び、たとえ人々が必ずしも正確にその働きを知っていなくても、それはハハをしている限りです。それで、なぜ私はこれらの小さなナゲットをこことそこに落としているのですか? –

関連する問題