Home > Blockchain >  how to copy files with extension .txt?
how to copy files with extension .txt?

Time:02-28

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

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

Turns out that glob returns the file with the full path. So, the value of archivo looks like "D:\2 PERSONA\[FILENAME].txt".

If you want to return only the filename, you should use os.chdir(path_input).before using glob or you can use replace - path_input by your path (up to you).

from os import chdir
from glob import glob
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'

chdir(path_input)

for archivo in glob("*.txt"):
    current = path_input   '\\'   archivo
    path_out = r'D:\Backup'   '\\'   str_date_backup   " - "   archivo
    copyfile(current, path_out)

This might work.

If you prefer to not change too much, here is the path_out edited to replace the current path for the backup path:

path_out = str(archivo).replace(path_input   '\\', r'D:\Backup'   '\\'   str_date_backup   " - ")
  • Related