gpt4 book ai didi

python - 为什么以下代码无法成功将所需文件移动到所需目录?

转载 作者:行者123 更新时间:2023-12-01 07:49:21 27 4
gpt4 key购买 nike

import os
import shutil
from collections import Counter as cnt


curr_path = os.getcwd()

ls = lambda path: [x for x in os.listdir(path) if x[0] != '.']

raw_files = [x for x in ls(curr_path) if '_' not in x]

filenames = []

folders = []

for f in raw_files: # sweeping out the '.', only having filenames

try:
i = f.index('.')
f_new = f[0:i]

filenames.append(f_new)
except ValueError:
filenames.append(f)

fname_freq = cnt(filenames)

for fname, freq in fname_freq.items():
if freq > 1:
folders.append(fname)


for fldr in folders:
print(fldr+'\n')
try:
os.makedirs(fldr+'_')
# adding a '_' to make the true folder name, i.e. foldername_ instead of foldername
except OSError:
pass

for f in raw_files:
print("File being analyzed is: {fn} \n".format(fn=f))
for fldr in folders:
print("Folder to move stuff to is {fold}\n".format(fold=fldr))
print("File {f} being checked . . . ".format(f=f))


if fldr in f and f[-1] != '_':
print("\t Moving file {fn} \n".format(fn=f))
shutil.move(f, fldr+'_')



上面是一个程序FileGrouper.py,它将在目录中查找是否存在多个具有共享名称的文件(即random.java、random.class)。如果有,那么它们将被移动到名为 [插入共享名]_ 的目录来组织它们。

此代码尤其拒绝对特定文件集起作用。

注意:我用占位符替换了我的实际用户名以及时间戳

-rwxr-xr-x  1 UserName  staff   8480 Month 20 Time:Time another_ptr_func
-rw-r--r--@ 1 UserName staff 324 Month 20 Time:Time another_ptr_func.c
-rwxr-xr-x 1 UserName staff 8572 Month 20 Time:Time arrays1
-rw-r--r-- 1 UserName staff 321 Month 20 Time:Time arrays1.c
-rw-r--r--@ 1 UserName staff 2058 Month 20 Time:Time file_grouper.py
-rwxr-xr-x 1 UserName staff 8432 Month 20 Time:Time forloop
-rw-r--r-- 1 UserName staff 119 Month 20 Time:Time forloop.c
-rwxr-xr-x 1 UserName staff 4248 Month 20 Time:Time gen_a
-rw-r--r-- 1 UserName staff 53 Month 20 Time:Time gen_a.c
-rwxr-xr-x 1 UserName staff 8432 Month 20 Time:Time hello
-rw-r--r-- 1 UserName staff 65 Month 20 Time:Time hello.c
-rwxr-xr-x 1 UserName staff 8432 Month 20 Time:Time hello2
-rw-r--r-- 1 UserName staff 140 Month 20 Time:Time hello2.c
-rw-r--r--@ 1 UserName staff 343 Month 20 Time:Time pointer.c
-rwxr-xr-x 1 UserName staff 8472 Month 20 Time:Time pointer_func
-rw-r--r-- 1 UserName staff 352 Month 20 Time:Time pointer_func.c
-rwxr-xr-x@ 1 UserName staff 19340 Month 20 Time:Time pointer_hex
-rwxr-xr-x 1 UserName staff 8432 Month 20 Time:Time switch
-rw-r--r--@ 1 UserName staff 219 Month 20 Time:Time switch.c
-rwxr-xr-x 1 UserName staff 8480 Month 20 Time:Time void_another_ptr_func
-rw-r--r-- 1 UserName staff 335 Month 20 Time:Time void_another_ptr_func.c

上面是此代码拒绝工作的特定文件集。我已经在以下内容上测试了相同的代码,其中包含大小为零的虚拟文件(使用 touch 创建它们):

-rw-r--r--  1 UserName  staff  1183 Month 20 Time:Time README.md
-rw-r--r--@ 1 UserName staff 2067 Month 20 Time:Time file_grouper.py
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random1
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random1.txt
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random2
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random2.txt
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random3
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random3.txt
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random4
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random4.txt
-rw-r--r-- 1 UserName staff 0 Month 20 Time:Time random5.txt

程序成功处理这些文件,并生成以下内容:

.
├── README.md
├── file_grouper.py
├── random1_
│   ├── random1
│   └── random1.txt
├── random2_
│   ├── random2
│   └── random2.txt
├── random3_
│   ├── random3
│   └── random3.txt
├── random4_
│   ├── random4
│   └── random4.txt
└── random5.txt

如您所见,程序成功忽略了单独的 random5.txt,因为不存在其他具有共享名称的文件。但是,其他共享名称确实存在的文件已成功分组到[插入共享名称]_

的文件夹中

最佳答案

如果“拒绝工作”是指跳过某些文件,那么可能是因为这一行:

raw_files = [x for x in ls(curr_path) if '_' not in x]

所有带下划线的文件都将被删除。

另一个问题是文件系统不允许文件和目录具有相同的名称,您可以通过附加下划线来覆盖它们。

我强烈建议使用pathlib库,它使您的代码更易于维护和可读:

from pathlib import Path


def file_grouper():
# Path to group
path = Path('.')

# List all files
ls = path.glob('*')

# Map all stems (file names without extension) to their file names
names = {}
for x in ls:
if x.is_file():
if x.stem not in names:
names[x.stem] = []
names[x.stem].append(x)

# Create and move
for stem, values in names.items():
if len(values) > 1:
(path / (stem + '_')).mkdir(exist_ok=True)
for value in values:
value.rename(path / (stem + '_') / value.name)


file_grouper()

关于python - 为什么以下代码无法成功将所需文件移动到所需目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56302815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com