拆分使用 “//”,并使用Python连接文件路径。

huangapple go评论62阅读模式
英文:

Split with "//" and join filepaths using python

问题

我有一些文件:

file1 = "C:\\temp\\IS_ModelDownload\\out"file2 = ["mapp\\here_Model_constants.gcd", "mapp\\here2_Model_constants.gcd"]
file3 = "glob\\here_test_constants.gcd"

我想要通过Python将它们分割并连接起来。注意:file2是包含2个文件路径的列表。

期望的输出应该是:output: C:\temp\IS_ModelDownload\out\mapp\here_Model_constants.gcd C:\temp\IS_ModelDownload\out\mapp\here2_Model_constants.gcd C:\temp\IS_ModelDownload\out\glob\here_test_constants.gcd

我尝试使用os.path.split("\\")来分割它们,我也知道join方法,但我在将它们全部组合在一起并按预期检索方面遇到了困难。

英文:

I have a few files as:

               file1=“C:\\temp\\IS_ModelDownload\\out” and

file2=[“mapp\\here_Model_constants.gcd”,"mapp\\here2_Model_constants.gcd"]    

  file3="glob\\here_test_constants.gcd"

I want to split them "//" and join them using python.Note:File2 is a list of 2 filepaths.
The desired output would be like:output: C:\temp\IS_ModelDownload\out\mapp\here_Model_constants.gcd C:\temp\IS_ModelDownload\out\mapp\here2_Model_constants.gcd C:\temp\IS_ModelDownload\out\glob\here_test_constants.gcd

I have tried using os. path. split("//") to split them and I'm aware of join method but I'm facing difficulty in combining altogether and retrive as desired.

答案1

得分: 1

你已经拥有一切所需的:

file1 = "C:\\temp\\IS_ModelDownload\\out"
file2 = ["mapp\\here_Model_constants.gcd", "mapp\\here2_Model_constants.gcd"]
file3 = "glob\\here_test_constants.gcd"

import os
print(os.path.join(file1, file2[0]))
print(os.path.join(file1, file2[1]))
print(os.path.join(file1, file3))

输出:

C:\temp\IS_ModelDownload\out\mapp\here_Model_constants.gcd
C:\temp\IS_ModelDownload\out\mapp\here2_Model_constants.gcd
C:\temp\IS_ModelDownload\out\glob\here_test_constants.gcd

请记住,虽然在创建字符串常量时我们必须使用两个反斜杠,但结果字符串中只有一个反斜杠。所以:

print(len("a\\b"))

这将打印出 3。这是理解在 Python 中处理文本的一个关键点。

作为替代,你可以使用“原始”字符串,告诉 Python 不要进行插值。例如,这将产生相同的输出:

file1 = r"C:\temp\IS_ModelDownload\out"
file2 = [r"mapp\here_Model_constants.gcd", r"mapp\here2_Model_constants.gcd"]
file3 = r"glob\here_test_constants.gcd"

import os
print(os.path.join(file1, file2[0]))
print(os.path.join(file1, file2[1]))
print(os.path.join(file1, file3))

此外,作为一则侧面说明,你上面的一些字符串使用了像你在文字处理应用程序中使用的“特殊”引号,其中开头引号和结束引号面向不同的方向。这些不是有效的 Python 语法。你不能使用类似 Word 这样的应用程序来编辑你的 Python 代码。

英文:

You already have everything you need:

file1 = "C:\\temp\\IS_ModelDownload\\out"
file2 = ["mapp\\here_Model_constants.gcd","mapp\\here2_Model_constants.gcd"]  
file3 = "glob\\here_test_constants.gcd"

import os
print(os.path.join(file1,file2[0]))
print(os.path.join(file1,file2[1]))
print(os.path.join(file1,file3))

Output:

C:\temp\IS_ModelDownload\out\mapp\here_Model_constants.gcd
C:\temp\IS_ModelDownload\out\mapp\here2_Model_constants.gcd
C:\temp\IS_ModelDownload\out\glob\here_test_constants.gcd

Remember, although we have to USE two backslashes when we create a string constant, the resulting string only has ONE backslash in it. So:

print( len("a\\b") )

That prints 3. This is a KEY point to understand working with text in Python.

As an alternative, you can use "raw" strings, which tell Python not to do that interpolation. For example, this produces the same output:

file1 = r"C:\temp\IS_ModelDownload\out"
file2 = [r"mapp\here_Model_constants.gcd",r"mapp\here2_Model_constants.gcd"]  
file3 = r"glob\here_test_constants.gcd"

import os
print(os.path.join(file1,file2[0]))
print(os.path.join(file1,file2[1]))
print(os.path.join(file1,file3))

Also, as a side note, several of your strings above are using "special" quote marks like you would use in a word processing app, where the opening quotes and the closing quotes face different directions. Those are not valid Python syntax. You can't use an application like Word to edit your Python code.

huangapple
  • 本文由 发表于 2023年6月16日 02:18:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定