如何复制扩展名为.txt的文件?

发布于 2025-01-10 14:31:11 字数 848 浏览 3 评论 0原文

我正在尝试创建一个程序来自动创建我在 python 中工作的目录的文件 .txt 。 我设法创建一个代码来查找所有扩展名为 .txt 的文件。但我无法将所有这些文件复制到另一个文件夹,因为它显示以下错误。 我将我的代码留给您,以便您可以帮助我解决它向我显示的错误。

import pathlib
from datetime import date
from shutil import  copyfile

date_backup = date.today()
str_date_backup = str(date_backup).replace('-','.')

path_input = r'D:\2 PERSONAL'
ruta = pathlib.Path(path_input)
for archivo in ruta.glob("**\\*.txt"):
    path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + archivo
    copyfile(path_input, path_out)

错误是:

Traceback (most recent call last):
  File "D:\5 PROYECTOS PYTHON\Automatizar_Backup\Automatizar_Backup.py", line 24, in <module>
    path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + archivo
TypeError: can only concatenate str (not "WindowsPath") to str

I am trying create a program to automatizate the creation of the files .txt of directory to my work in python.
I managed to create a code to find all the files with extension .txt . but but i can't copy all these files to another folder, because it shows me the following error.
I leave you my code, so that you can help me regarding the error that it shows me.

import pathlib
from datetime import date
from shutil import  copyfile

date_backup = date.today()
str_date_backup = str(date_backup).replace('-','.')

path_input = r'D:\2 PERSONAL'
ruta = pathlib.Path(path_input)
for archivo in ruta.glob("**\\*.txt"):
    path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + archivo
    copyfile(path_input, path_out)

The ERROR is:

Traceback (most recent call last):
  File "D:\5 PROYECTOS PYTHON\Automatizar_Backup\Automatizar_Backup.py", line 24, in <module>
    path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + archivo
TypeError: can only concatenate str (not "WindowsPath") to str

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2025-01-17 14:31:11

OS.walk() 通过自上而下或自下而上遍历树来生成目录树中的文件名。对于树中以目录顶部为根的每个目录(包括顶部本身),它会生成一个 3 元组(目录路径、目录名、文件名)。

你可以简单地使用这个:

import os
from datetime import date
from shutil import copyfile
 

date_backup = date.today()
str_date_backup = str(date_backup).replace('-','.')

path_input = 'D:\\2 PERSONAL'
for root, dirs, files in os.walk(path_input):#dir_path
    for file in files: 
        # change the extension from '.txt' to 
        # the one of your choice.
        if file.endswith('.txt'):
            path_current = root+'/'+str(file)
            path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + str(file)
            copyfile(path_current, path_out)

OS.walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at the directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

you can simply use this:

import os
from datetime import date
from shutil import copyfile
 

date_backup = date.today()
str_date_backup = str(date_backup).replace('-','.')

path_input = 'D:\\2 PERSONAL'
for root, dirs, files in os.walk(path_input):#dir_path
    for file in files: 
        # change the extension from '.txt' to 
        # the one of your choice.
        if file.endswith('.txt'):
            path_current = root+'/'+str(file)
            path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + str(file)
            copyfile(path_current, path_out)
旧时模样 2025-01-17 14:31:11

发生这种情况是因为在 pathlib.Path object 生成与模式(在您的情况下,“**\*.txt”)。幸运的是,这就像将 WindowsPath (或其他系统中的 PosixPath)转换为字符串一样简单:

for file in pathlib.Path(".").glob("*.txt"):
    file = str(file)

This happens because using glob on a pathlib.Path object yields all the files that match the pattern (in your case, "**\*.txt"). Luckily, this is as easy as converting your WindowsPath (or PosixPath in other systems) to string:

for file in pathlib.Path(".").glob("*.txt"):
    file = str(file)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文