for 将 str 转换为 list 为什么?

发布于 2025-01-04 09:12:28 字数 613 浏览 1 评论 0原文

我目前正在构建一个脚本来查找图像序列并将它们分组在一起。我的问题是当我获取文件名并对其运行“for in”时,它们会变成列表...为什么?这是我的代码:

import os, glob

path = r'C:\Users\manley\Desktop\testSeq'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):

    (dirName, fileName) = os.path.split(my_file)
    (fileBaseName, fExt) = os.path.splitext(fileName)
    #print fileName

    pathLists = fileName
    pathList1 = []
    pathList2 = []

    #print pathList
    #print fileName

    for fName in pathLists:
        print fName

当 fName 打印时,文件名 = rightSide_020_30.0001.jpg

我得到每个字母的列表...... 如何获取 fName 以将 fileName 保留为 str?

I'm currently building a script to find image sequences and group them together.. my problem is when I get the file names and go to run a "for in" on them they turn into lists... why?? This is my code:

import os, glob

path = r'C:\Users\manley\Desktop\testSeq'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):

    (dirName, fileName) = os.path.split(my_file)
    (fileBaseName, fExt) = os.path.splitext(fileName)
    #print fileName

    pathLists = fileName
    pathList1 = []
    pathList2 = []

    #print pathList
    #print fileName

    for fName in pathLists:
        print fName

The filename = rightSide_020_30.0001.jpg

when the fName prints I get a list of each letter...
How do I get the fName to perserve the fileName as a str?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与他有关 2025-01-11 09:12:28

您应该只这样做:

import os, glob

path = r'C:\Users\manley\Desktop\testSeq'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):

    (dirName, fileName) = os.path.split(my_file)
    print fileName

不需要更多代码。

You should only do this:

import os, glob

path = r'C:\Users\manley\Desktop\testSeq'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):

    (dirName, fileName) = os.path.split(my_file)
    print fileName

There's no need for more code.

孤云独去闲 2025-01-11 09:12:28

你的代码对我来说有点不清楚,但在你的情况下,每个字母都会被打印,因为你这样做

>>> mylist = "abc"
>>> for m in mylist:
...    print m
...
a
b
c
>>>

你的 pathlist 是一个字符串。

我认为你的意思是这样做:

pathList1 = []
pathList1.append(filename)

for fname in pathList1:
          print fname

Your code is a little unclear to me but in your case each letter is getting printed because your doing this

>>> mylist = "abc"
>>> for m in mylist:
...    print m
...
a
b
c
>>>

Your pathlist is a string.

I think you meant to do this:

pathList1 = []
pathList1.append(filename)

for fname in pathList1:
          print fname
高跟鞋的旋律 2025-01-11 09:12:28

pathLists = fileName 这是一个字符串...代表一个文件名...当您 for 遍历一个字符串时,它会产生每个字符...

pathLists = fileName which is a string... representing one file name... when you for over a string, it yields each character...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文