将不支持的文件扩展名移动 python

Moving unsupported file extensions by python

我正在尝试使用 Python - spyder 将文件从一个目录移动到另一个目录。 我的文件扩展名是 *.OD python 不支持或读取。

我试过使用通配符并省略文件扩展名(这不起作用)。此特定文件不能使用其他文件扩展名。 移动 python 支持的扩展名,如 .txt 和 .csv 工作正常。

import shutil
source = '//Original_Filepath/Extract*.od'
target = '//NewFilePath/Extract_*.od'

shutil.copy(source, target)

没有错误,只是 move/copy 文件不存在。

谢谢,

您尝试复制文件的方式存在一些基本错误。使用 shutil.copy 时,您不应指定 glob,而应指定确切的源和目标。

如果您想将一组文件从一个目录复制到另一个目录并(假设添加的下划线没有错误)更改目标,那么您应该尝试将 pathlibshutil(如果需要,还有 re)。

pathlib - Object-oriented filesystem paths

尝试调整这个:

import pathlib
import shutil
import re

source = pathlib.Path('//Original_Filepath') # pathlib takes care of end slash
source_glob = 'Extract*.od'
target = pathlib.Path('//NewFilePath')
for filename in source.glob(source_glob):
    # filename here is a Path object as well
    glob_match = re.match(r'Extract(.*)\.od', filename.stem).group(1)
    new_filename = "Extract_{}.od".format(glob_match)
    shutil.copy(str(filename), str(target / new_filename)) # `/` will create new Path

如果您对编辑目标或使用 pathlib 提供的任何其他高级功能不感兴趣,请参阅 Xukrao 的评论。

谢谢大家的帮助。非常感激! :)

我也可以使用以下内容复制文件(更简单一些)。 我省略了 * 并使用了日期字符串。

导入 shutil

从日期时间导入日期时间

现在=datetime.now()

Org_from=os.path.abspath('//原始文件路径')

New_to=os.path.abspath('//新路径')

shutil.copy(os.path.join(org_from, 'File_' + now.strftime("%Y%m%d") + '.od') , os.path.join(New_to, 'File_' + now.strftime("%Y%m%d") + '.od'))

干杯, 仁