Python でファイルの内容を確認し、同じフォルダーからファイルをコピーして新しい場所に移動するにはどうすればよいですか? 私は Python 3.1 を持っていますが、2.6 にも簡単に移植できます。
6254 次
2 に答える
3
例えば
import os,shutil
root="/home"
destination="/tmp"
directory = os.path.join(root,"mydir")
os.chdir(directory)
for file in os.listdir("."):
flag=""
#check contents of file ?
for line in open(file):
if "something" in line:
flag="found"
if flag=="found":
try:
# or use os.rename() on local
shutil.move(file,destination)
except Exception,e: print e
else:
print "success"
shutil docを見ると、.move() の下に
shutil.move(src, dst)¶
Recursively move a file or directory to another location.
If the destination is on the current filesystem, then simply use rename.
Otherwise, copy src (with copy2()) to the dst and then remove src.
copy2() を使用して別のファイルシステムに移動できると思います。
于 2010-02-12T02:06:29.053 に答える
1
于 2010-02-12T01:46:28.530 に答える