0
ファイルの一部を実行するforループを作成する方法はありますか? この場合、サブフォルダ内のすべての画像の1/5(ファイルの最後の1/5)をサブフォルダを持つ別のフォルダに移動したいと思います。私はいくつかを始めましたが、助けが必要です。Pythonを使ったファイルのループオーバーの場合
import os
path = 'pictures/'
outpath = 'oldpictures/'
for root, dirs, filenames in os.walk(path):
new_dir = root.replace(path, outpath, 1)
if not os.path.exists(new_dir):
os.mkdir(new_dir)
for filename in filenames: #Is it here that I can loop over the last fraction of all the files somehow?
os.system(mv {0} {1}.format(filename, new_dir)) #Dunno if this is the best way either or if it is possible.
最後の20%にする必要がありますか、それは単なる例です。任意の20%は簡単です...カウンタを保持し、トラバースするたびに5番目のファイルを移動するだけです。 –
ファイルの数が最初に 'cnt'と表示されますか? forループのカウンタを取得し、毎回 'counter + = 1'を実行します。 'counter> 0.8 * cnt'ファイルの移動を開始するたびに 'else continue ' – abhiieor