简体   繁体   English

从CSV读取文件名,然后将文件复制到其他目录

[英]Read filenames from CSV and then copy the files to different directory

I have been able to write a batch file to find files and put the file paths into a CSV. 我已经能够编写一个批处理文件来查找文件,并将文件路径放入CSV文件中。 I haven't been able to figure out how to read the file locations from the CSV and then move the files to a different storage device with the same folder structure using python. 我一直无法弄清楚如何从CSV读取文件位置,然后使用python将文件移动到具有相同文件夹结构的其他存储设备。 This is what I'd like to do. 这就是我想做的。

I wish I had some code to show you but none of it has worked. 我希望我有一些代码可以显示给您,但是没有一个起作用。

Here's a quick and dirty solution. 这是一个快速而肮脏的解决方案。 (I haven't tested it yet, YMMV!) (我尚未测试,YMMV!)

import csv
import os
import shutil
import sys


def main(argv):
  # TODO: this should do some error checking or maybe use optparse
  csv_file, existing_path_prefix, new_path_prefix = argv[1:]

  with open(csv_file, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
      # Assuming the column in the CSV file we want is the first one
      filename = row[0]

      if filename.startswith(existing_path_prefix):
        filename = filename[len(existing_path_prefix):]

      new_filename = os.path.join(new_path_prefix, filename)

      print ('Copying %s to %s...' % filename, new_filename),
      shutil.copy(filename, new_filename)
      print 'done.'
  print 'All done!'


if __name__ == '__main__':
  main(sys.argv)

Adding to Daniel's post, since he did warn he hadn't tested it :), I think you'll need to make a couple small changes. 在Daniel的帖子中,由于他确实警告过他尚未测试:),所以我认为您需要进行一些小改动。 Basically, I think the issue in the suggested code is that filename is assumed to be the full path. 基本上,我认为建议代码中的问题是假定filename是完整路径。 But then that creates a problem when you get to the os.path.join command for new_filename because you're adding a new path to a full path and name. 但是,当您进入os.path.join命令以获取new_filename时,这会产生问题,因为您要向完整路径和名称添加新路径。

I would suggest including a filepath and filename in your csv to make the code run. 我建议在csv中包含filename filepathfilename以使代码运行。 The changes appear to work when I testd it, although I didn't run as a function (and I used print() statements for Python 3.4 syntax): 尽管我没有作为函数运行(并且我为Python 3.4语法使用了print()语句print() ,但这些更改似乎在我测试时起作用了:

with open(csv_file, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
      # Assuming the columns in the CSV file we want are the first two  // Changed
      filename = row[0]
      filepath = row[1]   #Changed

     '''Changed: I skipped over this next part, didn't need it, but should be
     easy enough to figure out
     if filename.startswith(existing_path_prefix):
       filename = filename[len(existing_path_prefix):]
     '''

     new_filename = os.path.join(new_path_prefix, filename)

     print ('Copying %s to %s...' % filepath, new_filename)  #Changed
     shutil.copy(filepath, new_filename)   #Changed
     print 'done.'
print 'All done!'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从CSV读取文件名,然后将文件复制到其他目录-第2部分 - Read filenames from CSV and then copy the files to different directory -part 2 如何基于Python中文件名列表中的项目从目录复制文件 - How to copy files from a directory based on items in a list of filenames in Python 出现ValueError:试图将匹配的文件名从csv复制到另一个目录时,没有足够的值要解压(预期2,得到1) - Getting ValueError: not enough values to unpack (expected 2, got 1) while trying to copy matching filenames from csv to another directory 从多个csv文件中提取行和文件名 - extract rows and filenames from multiple csv files 如何从python目录中以相同顺序获取和读取csv文件 - How to get and read the csv files in same order from a directory in python 从目录中读取多个图像并将其转换为.csv文件 - Read multiple images from a directory and turn them into .csv files 如何使用 dask 从同一目录中读取多个 .csv 文件? - How read multiple .csv files from the same directory using dask? 使用 Python 从目录中读取所有 csv 个文件 - Read in all csv files from a directory using Python 从目录到.csv 列的文件名(每行文件名;无间隙行) - filenames from directory to .csv column (filename per row; no gap rows) 将来自不同目录路径的多个.csv文件与python合并 - Combine multiple .csv files with python from different directory paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM