[英]In Python, how do I pull the file name based on extension out of a text file and place in new text file?
I have a .txt file with several paths to files and different extensions. 我有一个.txt文件,其中包含多个文件路径和不同的扩展名。 Essentially it's a list of all resources used in a large project. 本质上,它是一个大型项目中使用的所有资源的列表。 There are often more than one file on a line. 一行上通常有多个文件。
What I want to do is get every file name with extension .png from this file and put them into a new text file. 我要做的是从该文件中获取每个扩展名为.png的文件名,并将其放入新的文本文件中。
This way I can take this .png file list, and compare it with the list I have already created which grabs all the file names and extensions of .png within my resource directory. 这样,我可以获取此.png文件列表,并将其与我已经创建的列表进行比较,该列表将获取资源目录中.png的所有文件名和扩展名。
The code I am using for that part is: 我用于该部分的代码是:
resourceList = [file for file in os.listdir('.')
if file.endswith('.png')]
My end goal is to compare file name lists and delete all the files from the resource directory that don't have a match. 我的最终目标是比较文件名列表,并从资源目录中删除所有不匹配的文件。
I have been playing with re
, glob
, and strip
with little success. 我一直在玩re
, glob
和strip
,但收效甚微。
Any help would be greatly appreciated. 任何帮助将不胜感激。 My programming skills are fair at best and I have been banging my head against this for a while now. 我的编程技能充其量是很普通的,并且我已经为此努力了一段时间。 Thanks! 谢谢!
UPDATE #2 更新#2
with open('Resource_PNGs.txt') as f:
for line in f:
if '.png' in line:
(head, tail) = os.path.split(line)
print('%s' % (tail))
To Clarify: The resource list I have figured out is a list of file names taken out of a directory. 澄清一下:我想出的资源列表是从目录中取出的文件名列表。 Example: 'Button.png', 'Switch.png', etc
. 例如: 'Button.png', 'Switch.png', etc
。 The one I am having trouble with is a text file copied from a search done in Visual Studio for '*.png' so I have the file the .png is being referenced in with path, some other C++ garbage, and the filename.png that i am looking to capture. 我遇到麻烦的是一个文本文件,该文件是在Visual Studio中搜索“ * .png”后复制的一个文本文件,因此,我使用路径,其他一些C ++垃圾和filename.png引用了.png文件。我想要捕获的。 I eventually would like to compare the 2 lists, and see which files don't have a match. 我最终想比较两个列表,看看哪些文件不匹配。 The lines look like this: 这些行如下所示:
C:\BaseProjects\Source\FileManagerGeneralSettings.qml(86): buttonImageName: imgDirUrlString + "/WizBack_titleBar.png"
C:\BaseProjects\Source\FileSendPage.qml(35): source: imgDirUrlString + "/TabEmpty.png"
C:\BaseProjects\Source\FileSendPage.qml(224): buttonIcon: imgDirUrlString + "/DialogConfirm.png"
You should look at the os.path module. 您应该查看os.path模块。 It does all the managing of file paths and directories across platforms. 它负责跨平台的文件路径和目录的所有管理。
To get the file name except the extension look at basename 要获取除扩展名之外的文件名,请查看基本名
On in this case use join to add .png to the end of all your entries in the list to be compared as this will mean the filenames are complete and can be passed to the delete function. 在这种情况下,请使用join将.png添加到要比较的列表中所有条目的末尾,因为这将意味着文件名完整并且可以传递给delete函数。
Yea! 是啊!
I am one step closer to my final goal. 我距离最终目标只有一步之遥。 I now have two lists that I can iterate through comparing the strings to find which ones match. 现在,我有两个列表,可以通过比较字符串以找出匹配的列表进行迭代。
To create a list of .pngs within the directory I am using: 在我正在使用的目录中创建.png列表:
resourceList = [file for file in os.listdir('.') if file.endswith('.png')]
To create the second list which pulls all the .pngs out of a mess of text in a text file I am using this: 要创建第二个列表,它将所有.png文件从文本文件中的一堆文本中提取出来,我正在使用以下命令:
with open('Resource_PNGs.txt', 'rU') as f:
for line in f:
usedPngs = (re.findall('([-\w]+\.(?:png))', f.read()))
I was not aware of all the different "wildcard" characters for strings, but it seemed like the only to do it. 我不知道字符串的所有不同“通配符”,但这似乎是唯一的方法。 I was having little luck with os.path
because it was giving me all the other paths found. 我对os.path
不太满意,因为它给了我所有其他找到的路径。 I'm sure with some of the same re
magic I could have gotten it. 我敢肯定有一些相同的re
神奇,我可以得到它。
Hopefully my problem can help others. 希望我的问题可以帮助其他人。 Now off to comparing! 现在开始进行比较!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.