我正在处理大文件,这里我的问题是双重的。
Bash – 为了testing的目的,我想遍历给定目录中的每个文件,取每个文件的Head
(比如Head 10000
),并留下每个文件的精简版。 无论是在同一个目录或另一个它并不重要,虽然我想同样会是首选。
Python3 – 我怎样才能做到这一点编程? 我想我需要使用os模块 ?
试试这个使用shell :
for i in *; do cp "$i" "$i.tail" sed -i '10001,$d' "$i.tail" done
或者干脆:
for i in *; do sed '10001,$d' "$i" > "$i.tail" done
要么 :
for i in *; do head -n 1000 "$i" > "$i.tail" done
对于python,请参阅http://docs.python.org/2/library/subprocess.html如果你想使用shell代码。
击:
最直接的方法是:
#!/usr/bin/env bash DEST=/tmp/ for i in * do head -1000 "${i}" > ${DEST}/${i} done
如果您有大量文件,则可以通过生成一个文件列表,将它们分开,然后对每个列表运行循环来运行多个作业。
蟒蛇:
假设目标是不产生shell会话来执行外部二进制文件,比如“head”,这就是我如何去做的。
#!/usr/bin/env python import os destination="/tmp/" for file in os.listdir('.'): if os.path.isfile( file ): readFileHandle = open(file, "r") writeFileHandle = open( destination + file , "w") for line in range( 0,1000): writeFileHandle.write(readFileHandle.readline()) writeFileHandle.close() readFileHandle.close()
要以这种方式缩写当前目录中的所有文件,可以使用:
for f in *; do [[ $f != *.small ]] && head -n 10000 "$f" > "$f".small; done
这些文件将以.small
作为后缀。
为了从python这样做,
import os os.system('for f in *; do [[ $f != *.small ]] && head -n 10000 "$f" > "$f".small; done')