英文:
Moving files from one dir to the new dir. Removing unneeded subfolders when replicating file path?
问题
import os
import shutil
source_dir = "folder\\"
dest_dir = "newfolder\\"
txt_files = [f for f in files if f.endswith('.txt')]
if txt_files:
rel_dir = os.path.relpath(root, source_dir)
dest_subdir = os.path.join(dest_dir, rel_dir)
os.makedirs(dest_subdir, exist_ok=True)
for file in txt_files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_subdir, file)
shutil.move(src_file, dest_file)
print(f"Moved file {file} to {dest_subdir}")
dirs.clear()
Edit: To remove the subfolder without specifying a predefined string, you can modify the code as follows:
rel_dir = os.path.relpath(root, source_dir)
# Split the directory path into parts
path_parts = rel_dir.split(os.sep)
# Remove the subfolder
if len(path_parts) > 2:
dest_subdir = os.path.join(dest_dir, path_parts[0], path_parts[-1])
else:
dest_subdir = os.path.join(dest_dir, rel_dir)
os.makedirs(dest_subdir, exist_ok=True)
for file in txt_files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_subdir, file)
shutil.move(src_file, dest_file)
print(f"Moved file {file} to {dest_subdir}")
dirs.clear()
This modified code will remove the subfolder inside the AAAA folder dynamically without relying on a predefined string.
英文:
Im trying to move files inside source_dir that has a folder structure like (folder>AAAA>03-03-2023>file.txt). I want to move the AAAA Folder and the file.txt to the new destination (dest_dir). Right now my code is basically replicating my original folder structure and moving the .txt and that's fine but I just want to remove the sub folder inside the AAAA folder. How can I do this?
import os
import shutil
source_dir = "folder\\"
dest_dir = "newfolder\\"
txt_files = [f for f in files if f.endswith('.txt')]
if txt_files:
# create the destination directory path
rel_dir = os.path.relpath(root, source_dir)
dest_subdir = os.path.join(dest_dir, rel_dir)
# create the destination directory if it doesn't exist
os.makedirs(dest_subdir, exist_ok=True)
# move the files to the destination directory while preserving the directory structure
for file in txt_files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_subdir, file)
shutil.move(src_file, dest_file)
print(f"Moved file {file} to {dest_subdir}")
# stop looking inside this folder and its subdirectories
dirs.clear()
Edit: Adding the .replace code and giving the string name of the subfolder, removes the subfolder. This is great but is there a way for the code to handle it without giving a predefined string?
rel_dir = os.path.relpath(root, source_dir)
dest_path = os.path.join(dest_dir, rel_dir).replace('03-03-2023', '')
os.makedirs(dest_path, exist_ok=True)
</details>
# 答案1
**得分**: 0
成功修复了这个问题。只是添加了这段代码。
旧代码:
```python
rel_dir = os.path.relpath(root, source_dir)
dest_subdir = os.path.join(dest_dir, rel_dir)
新代码:
if os.path.sep in rel_dir:
rel_dir = rel_dir.split(os.path.sep)[0]
dest_subdir = os.path.join(dest_dir, rel_dir)
我试图在文件移动之后删除文件夹,但在文件移动期间处理并删除子文件夹要好得多。
英文:
Managed to fix this. Just added this code.
old code:
rel_dir = os.path.relpath(root, source_dir)
dest_subdir = os.path.join(dest_dir, rel_dir)
new code:
if os.path.sep in rel_dir:
rel_dir = rel_dir.split(os.path.sep)[0]
dest_subdir = os.path.join(dest_dir, rel_dir)
I was trying to remove folders after file had moved. But was a lot better to handle and remove subfolders during file moving.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论