简体   繁体   English

您如何使用Python从CSV打印文件列表?

[英]How do you use Python to print a list of files from a CSV?

I was hoping to use a CSV file with a list of file paths in one column and use Python to print the actual files. 我希望使用一列中包含文件路径列表的CSV文件,并使用Python打印实际文件。

We are using Window 7 64-bit. 我们正在使用Window 7 64位。


I have got it to print a file directly: 我有它直接打印文件:

import os
os.startfile(r'\\fileserver\Sales\Sell Sheet1.pdf, 'print')

The issues comes in when I bring in the CSV file. 当我导入CSV文件时,就会出现问题。 I think I'm not formatting it correctly because I keep getting: 我认为我的格式不正确,因为我不断得到:

FileNotFoundError: [WinError2] The system cannot find the file specified: "['\\\\\\\\fileserver\\\\Sales\\\\Sell Sheet1']" FileNotFoundError:[WinError2]系统找不到指定的文件:“ ['\\\\\\\\ fileserver \\\\ Sales \\\\ Sell Sheet1']”

This is where I keep getting hung up: 这是我一直挂断电话的地方:

import os
import csv
with open (r'\\fileserver\Sales\TestList.csv') as csv_file:
    TestList = csv.reader(csv_file, delimiter=',')
    for row in TestList:
        os.startfile(str(row),'print')

My sample CSV file contains: 我的示例CSV文件包含:

\\fileserver\Sales\SellSheet1
\\fileserver\Sales\SellSheet2
\\fileserver\Sales\SellSheet3

Is this an achievable goal? 这是可以实现的目标吗?

You shouldn't be using str() there. 您不应该在那里使用str() The CSV reader gives you a list of rows, and each row is a list of fields. CSV阅读器为您提供行列表,每行都是一个字段列表。 Since you just want the first field, you should get that: 由于您只想要第一个字段,因此应该得到:

os.startfile(row[0], 'print')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM