如何使用 Shutil 复制包含特定字符串的文件?
How to copy files containing certain string using Shutil?
我的任务是编写一个非常简单的脚本,但我一辈子都想不出如何...
我需要做的就是使用模块 Shutil 将包含 "Tax_" 的所有文件从 ~/Documents 文件夹复制到我当前的工作目录。
import shutil
import os
src = os.path.expanduser('~/Documents/Tax 2018')
dst = os.getcwd()
for files in src:
if 'bdd' in files: # also tried: if files.startswith("Tax_")
shutil.copy(files,dst)
无效。我们还可以选择使用 Shutil.which("Tax_")
查找文件,但这根本行不通。
您可以尝试这样的操作:
from glob import glob
for file in glob('~/Documents/Tax 2018/Tax_*'):
shutil.copy(file, dst)
glob
将 return 匹配通配符模式的文件列表,在这种情况下,这些文件在给定路径上以 Tax_ 开头。
也许这样的事情对你有用。它要求您提供输入目录,然后询问您要将文件保存到哪里。如果保存目录不存在则创建它,如果保存目录已经存在则它只是继续。最后的 input()
函数就在那里离开 python 控制台所以你可以看到它已经完成。
使用 shutil.copy2 的好处是它会尝试保留文件的元数据。
另外,根据您的文件命名方式,您不是很具体,您可能需要稍微更改此行 if 'tax_' in file.lower():
。
import shutil
import os
input_dir = input('Please enter directory that contains that tax files.\n')
output_dir = input('\nPlease enter the path where you want to save the files.\n')
for file in os.listdir(input_dir):
if 'tax_' in file.lower():
if not os.path.exists(output_dir):
os.makedirs(output_dir)
shutil.copy2(os.path.join(input_dir, file), os.path.join(output_dir, file))
input('\nFinished --> Files are saved to %s' % output_dir) # format() or f strings are ideal but not sure which python version you have
确保为文件指定文件类型,希望这对您有用:
#!/usr/bin/env python3
#Imports
from shutil import copyfile
import shutil
import os
# Assigning the variables to search and the location
file_str = "Tax_201"
search_path = os.path.join(os.path.expanduser('~'),'Documents')
# Repeat for each file in the search path
for filename in os.listdir(path=search_path):
# Check if the file starts with Tax_2018
if filename.startswith(file_str):
# Assigning the variables for the src and destination
path_src = os.path.join(os.path.expanduser('~'),'Documents', filename)
path_destination = os.path.join(os.path.expanduser('~'),'Documents', 'Taxes', filename)
# Copy the file that matched the file_str
shutil.copyfile(path_src, path_destination)
else:
# Stop the loop
break
输出
Documents/
├── Taxes
├── Tax_2018.txt
├── Tax_2019.txt
Documents/Taxes/
├── Tax_2018.txt
├── Tax_2019.txt
请尝试:
import glob
import os
import shutil
dst = os.getcwd()
str_target = 'bdd' # OR 'Tax_'
f_glob = "~/Documents/Tax 2018/{}*.txt".format(str_target)
ls_f_dirs = glob.glob(f_glob)
for f_dir in ls_f_dirs:
shutil.copy(f_dir, dst)
我的任务是编写一个非常简单的脚本,但我一辈子都想不出如何...
我需要做的就是使用模块 Shutil 将包含 "Tax_" 的所有文件从 ~/Documents 文件夹复制到我当前的工作目录。
import shutil
import os
src = os.path.expanduser('~/Documents/Tax 2018')
dst = os.getcwd()
for files in src:
if 'bdd' in files: # also tried: if files.startswith("Tax_")
shutil.copy(files,dst)
无效。我们还可以选择使用 Shutil.which("Tax_")
查找文件,但这根本行不通。
您可以尝试这样的操作:
from glob import glob
for file in glob('~/Documents/Tax 2018/Tax_*'):
shutil.copy(file, dst)
glob
将 return 匹配通配符模式的文件列表,在这种情况下,这些文件在给定路径上以 Tax_ 开头。
也许这样的事情对你有用。它要求您提供输入目录,然后询问您要将文件保存到哪里。如果保存目录不存在则创建它,如果保存目录已经存在则它只是继续。最后的 input()
函数就在那里离开 python 控制台所以你可以看到它已经完成。
使用 shutil.copy2 的好处是它会尝试保留文件的元数据。
另外,根据您的文件命名方式,您不是很具体,您可能需要稍微更改此行 if 'tax_' in file.lower():
。
import shutil
import os
input_dir = input('Please enter directory that contains that tax files.\n')
output_dir = input('\nPlease enter the path where you want to save the files.\n')
for file in os.listdir(input_dir):
if 'tax_' in file.lower():
if not os.path.exists(output_dir):
os.makedirs(output_dir)
shutil.copy2(os.path.join(input_dir, file), os.path.join(output_dir, file))
input('\nFinished --> Files are saved to %s' % output_dir) # format() or f strings are ideal but not sure which python version you have
确保为文件指定文件类型,希望这对您有用:
#!/usr/bin/env python3
#Imports
from shutil import copyfile
import shutil
import os
# Assigning the variables to search and the location
file_str = "Tax_201"
search_path = os.path.join(os.path.expanduser('~'),'Documents')
# Repeat for each file in the search path
for filename in os.listdir(path=search_path):
# Check if the file starts with Tax_2018
if filename.startswith(file_str):
# Assigning the variables for the src and destination
path_src = os.path.join(os.path.expanduser('~'),'Documents', filename)
path_destination = os.path.join(os.path.expanduser('~'),'Documents', 'Taxes', filename)
# Copy the file that matched the file_str
shutil.copyfile(path_src, path_destination)
else:
# Stop the loop
break
输出
Documents/ ├── Taxes ├── Tax_2018.txt ├── Tax_2019.txt
Documents/Taxes/ ├── Tax_2018.txt ├── Tax_2019.txt
请尝试:
import glob
import os
import shutil
dst = os.getcwd()
str_target = 'bdd' # OR 'Tax_'
f_glob = "~/Documents/Tax 2018/{}*.txt".format(str_target)
ls_f_dirs = glob.glob(f_glob)
for f_dir in ls_f_dirs:
shutil.copy(f_dir, dst)