简体   繁体   English

有没有办法使用 windows Python 打印任何文件夹中的所有内容?

[英]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!请协助,我将不胜感激!

  1. Have you tried this solution ?你试过这个解决方案吗? It aperas to be as easy as它很容易
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 系统上,我无法对其进行测试,但互联网似乎同意。

  1. Once you know how to print one, printing many should not be any harder than iterating over the contents of the directory and filtering for files.一旦你知道如何打印一个,打印多个应该不会比遍历目录内容和过滤文件更难。 For example, since you already have 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.

相关问题 有没有一种方法可以使用Python对一个文件夹中的所有pdf文件进行OCR? - Is there a way to OCR all pdf files within one folder using Python? Python-用户输入目录中的特定文件夹,然后打印目录的所有内容 - Python - User input for specific folder in directory, then print all contents of directory 如何在不删除文件夹的情况下删除文件夹中的内容 python - How to delete the contents within a folder without deleting the folder using python 如何使用python读取Windows历史记录(文件夹)的内容? - How do I read the contents of Windows History (folder) using python? 我的cmd有没有办法从Python中打印出任何unicode? - Is there a way for my cmd to print out any unicode from within Python? 有什么方法可以在不使用 Python 中的任何字符串或数字的情况下打印某些内容? - Is there any way to print something without using any strings or numbers in Python? 如何使用 python 真正删除文件夹中的所有文件和 Windows 上的文件夹? - How to really delete all files in a folder and the folder on Windows using python? 使用python复制文件夹和内容 - Copying folder and contents using python 有没有办法在 Python 中打印 **kwargs - Is there any way to print **kwargs in Python Pathlib - 打印文件夹的内容 - Pathlib - print contents of a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM