我尝试创建一个脚本来计算文件夹中所有文件中的多个字符,并将少于50个字符的文件移动到另一个文件夹。
import os
newPath = "E:\\Sorteret 3"
for root, dirs, files in os.walk("."):
for fileName in files:
if(fileName == 'sort.py'): continue
words=line.split()
if len(words) < 50 in open(os.path.join(root, fileName), 'r', encoding="Latin-1").read():
os.rename(os.path.join(root, fileName), os.path.join(newPath, fileName))`
我收到以下错误:
"Name line is not defined".
答案 0 :(得分:0)
你没有阅读任何内容。 我会阅读该文件的所有内容,然后进行所有检查:
import os
newPath = "<your path>"
oldPath = "<your path>"
for root, dirs, files in os.walk(oldPath):
for fileName in files:
if(fileName == 'sort.py'):
continue
fpath = os.path.join(root, fileName)
len = os.stat(fpath).st_size
if len < 50:
os.rename(fpath, os.path.join(newPath, fileName))
希望这有帮助