[英]Is there a way to print all contents within any folder using windows Python?
I am a beginner trying to learn to code.我是一个尝试学习编码的初学者。 So far, I am just trying to print a file first, before printing everything within a folder.到目前为止,我只是想先打印一个文件,然后再打印文件夹中的所有内容。 I know how to import the file just to look at it.我知道如何导入文件只是为了查看它。
To be more specific I am trying to print the file to my printer which is connected through Wi-Fi.更具体地说,我正在尝试将文件打印到通过 Wi-Fi 连接的打印机。
What I am asking:我在问什么:
1.What code is needed to print (hard copy to printer) 1 file? 1.打印(硬拷贝到打印机)1个文件需要什么代码?
2.What code is needed to print all files within a folder (hard copies to printer)? 2.打印文件夹中的所有文件需要什么代码(硬拷贝到打印机)?
Please Assist, I would greatly appreciate it!请协助,我将不胜感激!
import os
os.startfile("C:/Users/TestFile.txt", "print")
Since I'm not on a Windows system, i can't test it, but the internet seems to agree.由于我不在 Windows 系统上,我无法对其进行测试,但互联网似乎同意。
os
imported:例如,由于您已经导入了os
:for file in os.listdir(base_dir):
file_name = os.path.join(base_dir, file)
if os.path.isfile(file):
os.startfile(file, "print")
You can also try learning Pathlib , but for such as simple use case, the advantages of it over os
are not that big.你也可以尝试学习Pathlib ,但是对于简单的用例来说,它相对于os
的优势并没有那么大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.