简体   繁体   English

从一个文件中打开多个文件

[英]Open multiple files from a file

I need to open a file that have multiple absolute file directories. 我需要打开一个具有多个绝对文件目录的文件。

EX: 例如:

Layer 1 = C:\\User\\Files\\Menu\\Menu.snt 第1层= C:\\ User \\ Files \\ Menu \\ Menu.snt

Layer 2 = C:\\User\\Files\\N0 - Vertical.snt 第2层= C:\\ User \\ Files \\ N0-Vertical.snt

The problem is that when I try to open C:\\User\\Files\\Menu\\Menu.snt python doesn't like \\U or \\N 问题是当我尝试打开C:\\ User \\ Files \\ Menu \\ Menu.snt时, python不喜欢\\ U\\ N

I could open using r"C:\\User\\Files\\Menu\\Menu.snt" but I can't automate this process. 我可以使用r"C:\\User\\Files\\Menu\\Menu.snt"但无法自动执行此过程。

file = open(config.txt, "r").read()
list = []

for line in file.split("\n"):
    list.append(open(line.split("=",1)[1]).read())

It prints out: 它输出:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 33-34: malformed \N character escape

The backslash character \\ is used as an escape character by the Python interpreter in order to provide special characters. Python解释程序将反斜杠字符\\用作转义字符,以提供特殊字符。

For example, \\n is a "New Line" character, like you would get from pressing the Return key on your keyboard. 例如, \\n是“换行”字符,就像您按键盘上的Return键一样。

So if you are trying to read something like newFolder1\\newFolder2 , the interpreter reads it as: 因此,如果您尝试读取诸如newFolder1\\newFolder2 ,则解释器将其读取为:

newFolder1
ewFolder2

where the New Line character has been inserted between the two lines of text. 在两行文本之间插入了换行符的位置。

You already mentioned one workaround: using raw strings like r'my\\folder\\structure' and I'm a little curious why this can't be automated. 您已经提到了一种解决方法:使用原始字符串(例如r'my\\folder\\structure' ,我有点好奇为什么这不能自动化。

If you can automate it, you could try replacing all instances of a single backslash ( \\ ) with a double backslash ( \\\\ ) in your file paths and that should work. 如果可以自动执行,则可以尝试在文件路径中用双反斜杠( \\\\ )替换单个反斜杠( \\ )的所有实例,这应该可以工作。

Alternatively, you can try looking in the os module and dynamically building your paths using os.path.join() , along with the os.sep operator. 另外,您可以尝试查看os模块,并使用os.path.join()os.sep运算符动态构建路径。

One final point: You can save yourself some effort by replacing: 最后一点:您可以通过替换以下内容来节省一些精力:

list.append(open(line.split("=",1)[1]).read())

by 通过

list = open(line.split("=",1)[1]).readlines()

here is my solution: 这是我的解决方案:

file = open("config.txt", "r").readlines()
list = [open(x.split("=")[1].strip(), 'r').read() for x in file]

readlines creates a list that contains all lines in file, there is no need to split the whole string. readlines创建一个包含文件中所有行的列表,无需拆分整个字符串。

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

相关问题 从文件管理器打开多个文件 - Open multiple files from file managers 如何从具有 python 文件列表的单个文件中打开多个文件以及如何对它们进行处理? - how to open multiple file from single file which is having list of files in python and how to do processing on them? 从文件夹中打开并读取多个xml文件 - Open and read multiple xml files from the folder 从文件夹中打开并解析多个XML文件 - Open and parse multiple XML files from a folder 如何从数组中打开多个文本文件? - How to open multiple text files from an array? 如何从文件对话框中选择多个文件并同时打开并访问它们 - How to choose multiple files from file dialog and open at the same time and access them 如何使用 python 中 tsv 文件中的名称从一个文件夹中打开多个文件? - How to open multiple files from a folder using the names in tsv file in python? 如何从列表中加载多个 JSON 文件并使用 python 分别打开每个文件? - How to load multiple JSON files from a list and open each file separately using python? 使用 xarray 和 open_mfdataset 从 url 打开多个文件 - Open multiple files from urls using xarray and open_mfdataset 如何从文件路径列表中打开文件 - How to open files from a list of file paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM