0
ディレクトリ内のすべてのファイルに対して実行したいスクリプトがあります。私が知っている使用ファイルが見つかるすべての例は、もはやpython 3.xでは動作しません。ディレクトリ内の複数のファイルに対してスクリプトを実行するにはどうすればよいですか?
だから、これは私がすることを実現する前に試したものです:
import sys, os, cv2
path = 'C:\\Users\\telli\\Desktop\\Test Shapes\\Shapes\\Squares'
for filename in os.listdir(path):
for file in files:
if file.endswith('.jpg'):
os.system ('C:\\Users\telli\\Desktop\\test.py {}'.format(root + '\\' + file))
は、だから私はそれをどのように変更すべきか?私はopen
を使用して見たことがあるが、私は正確に暑いとは思わない。
また、メインフォルダ内の特定のファイル/フォルダを除外するコード行を表示することはできますか?
私はこれを試してみましたが、何も起こりません:import sys, os
path = 'C:\\Users\\telli\\Desktop\\Test Shapes\\Shapes\\Squares'
for file in os.listdir(path):
if file.endswith('.jpg'):
img = cv2.imread(file)
#print(img)
#Converting the image to Grayscale
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(grey,127,255,1)
im2,contours, h = cv2.findContours(thresh, 1, cv2.CHAIN_APPROX_SIMPLE)
contours.sort(key = len)
cv2.imshow('img',img)
cv2.waitKey(0)
#os.system('C:\\Users\telli\\Desktop\\test.py {}'.format(root + '\\' + file))
おかげ
'this_is_a_bad_file.gif' 排除するあなたが持っている実際の問題は何ですか? – smarx
Python 2の 'file'関数はここでは使われていません...このコードはPython 3で動作するはずです(少なくとも、問題は'ファイル '関数ではありません) –
なぜ内部ループですか?それがあなたの問題です。 'os.listdir(path)内の'のために使用して、内側のループを削除してください。 –