如何从文件夹中获取大于00918.xlsb的所有文件名(例如:01234.xlsb)?

发布于 2025-02-10 15:21:56 字数 264 浏览 2 评论 0原文

我有一个文件夹,其中数千个文件,从00001.xlsb,00002.xlsb到12428.xlsb(每天增加)。 在python中,要获取我使用的所有列表(Fichier表示文件,用法语):

fichiers = np.array([fichier for fichier in glob.glob("./brut/" + "/*.xlsb")])

我获取所有文件,但我不希望从00001.xlsb到00918.xlsb。 如何在上面写下这样的条件?

I have a folder with thousands of files, from 00001.xlsb, 00002.xlsb to 12428.xlsb (increasing everyday).
In Python, to get the list of all of them I use (fichier means file, in French):

fichiers = np.array([fichier for fichier in glob.glob("./brut/" + "/*.xlsb")])

I get all files but I don't want files from 00001.xlsb to 00918.xlsb
How can I write such a condition in the above?

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

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

发布评论

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

评论(2

末が日狂欢 2025-02-17 15:21:56

也许是这样:

import os,glob

def castAsInt(fileName):
    #manage the exception if file name cannot  be casted as int => return 0
    try:
        return int (fileName.split(".")[0])
    except :
        return 0


expectedDir = "/home/renaud/Bureau/Python/Test/*" 
MinValue= 918

#All Files
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True)]
print("All Files")
print(fileList)

#Absolute links
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print("Absolutes Links")
print(fileList)

#Relaive links
print("Relative Links")
fileList=[fichier for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print(fileList)

结果:

All Files
['0002XXX.ods', '1000.ods', '1001.ods', '0002.ods', '0001.ods']
Absolutes Links
['1000.ods', '1001.ods']
Relative Links
['/home/renaud/Bureau/Python/Test/1000.ods', '/home/renaud/Bureau/Python/Test/1001.ods']

Maybe something like that:

import os,glob

def castAsInt(fileName):
    #manage the exception if file name cannot  be casted as int => return 0
    try:
        return int (fileName.split(".")[0])
    except :
        return 0


expectedDir = "/home/renaud/Bureau/Python/Test/*" 
MinValue= 918

#All Files
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True)]
print("All Files")
print(fileList)

#Absolute links
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print("Absolutes Links")
print(fileList)

#Relaive links
print("Relative Links")
fileList=[fichier for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print(fileList)

Result:

All Files
['0002XXX.ods', '1000.ods', '1001.ods', '0002.ods', '0001.ods']
Absolutes Links
['1000.ods', '1001.ods']
Relative Links
['/home/renaud/Bureau/Python/Test/1000.ods', '/home/renaud/Bureau/Python/Test/1001.ods']
赠意 2025-02-17 15:21:56

在下面添加另一行:

fichiers = [fichier for fichier in fichiers if int(fichier.split('.')[0]) > 918]

Add another line below that:

fichiers = [fichier for fichier in fichiers if int(fichier.split('.')[0]) > 918]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文