简体   繁体   English

如何从子文件夹访问文件,该子文件夹位于最近使用python创建或修改的文件夹中?

[英]How to access files from a subfolder that lies into a folder which recently created or modified using python?

I am performing web test automation using selenium Webdriver. 我正在使用Selenium Webdriver执行Web测试自动化。 I am pretty much new to python and Web test. 我对python和Web测试非常陌生。 The question is : I want to write a Python code that automatically opens the files in following way: 问题是: 我想编写一个Python代码,该代码以以下方式自动打开文件:

"C:\\Software\\" this path has many folders like “ C:\\ Software \\”此路径具有许多文件夹,例如

C:\Software\ 
          AB_101_B1\
                AB_101_B1\
                    *.bin
                    *.hex
                    *.hex
                AB_101_B1.zip

          AB_101_B2\
               AB_101_B2\
                   *.bin
                   *.hex
                   *.hex
               AB_101_B2.zip

          AB_102_B1\
               AB_102_B1\
                   *.bin
                   *.hex
                   *.hex
               AB_102_B1.zip
          ...

         AB_103_B7\
               AB_103_B7\
                   *.bin
                   *.hex
                   *.hex
               AB_103_B1.zip

Here AB_103_B7 is latest created or modified folder. 这里AB_103_B7是最新创建或修改的文件夹。 Each folder contains one subfolder with same name as main folder(eg:AB_103_B7) and one ZIP folder (AB_103_B7.zip). 每个文件夹包含一个与主文件夹同名的子文件夹(例如:AB_103_B7)和一个ZIP文件夹(AB_103_B7.zip)。 The subfolder and ZIP folder contains 3 files with different extensions(Example: .bin, .hex , .hex) 子文件夹和ZIP文件夹包含3个具有不同扩展名的文件(例如.bin,.hex和.hex)

I want to access latest modified or created folder then automatically upload files from this subfolder and also upload zip folder into Website. 我想访问最新的修改或创建的文件夹,然后从该子文件夹自动上传文件,还将zip文件夹上传到网站。

Note: The main folder " AB_* " keeps updating. 注意:主文件夹“ AB_ * ”不断更新。 So code must detect the latest folder either by name or by modified time. 因此,代码必须通过名称或修改时间来检测最新文件夹。 I could directly upload file by setting directory, Example: driver.find_element_by_id('abc').send_keys("C:\\software\\AB_103_B7\\AB_103_B7\\test.bin") . 我可以通过设置目录直接上传文件,例如: driver.find_element_by_id('abc').send_keys("C:\\software\\AB_103_B7\\AB_103_B7\\test.bin") But my problem is to access the latest folder. 但是我的问题是访问最新文件夹。

Could any help me with coding in python? 有什么可以帮助我进行python编码吗? I checked os.walk() from other questions. 我从其他问题检查了os.walk() I am confused about what should be written for dirname, subdirs, files . 我对应该为目录名,子目录,文件写什么感到困惑。

Try this code and let me know in case of any issues: 尝试使用此代码,如有任何问题,请通知我:

import os
path = "C:\\Software\\"
latest_modified_folder = ''
time_of_modification = 0

for root, subdirs, files in os.walk(path):
    for subdir in subdirs:
        if os.path.getmtime(os.path.join(root, subdir)) > time_of_modification:
            time_of_modification = os.path.getmtime(os.path.join(root, subdir))
            latest_modified_folder = os.path.join(root, subdir)

for file_ in os.listdir(latest_modified_folder):
    # I'm not sure about how you want to upload all files from folder, so...
    driver.find_element_by_id('abc').send_keys(os.path.join(latest_modified_folder, file_))  

暂无
暂无

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

相关问题 如何使用 Python 从文件夹或子文件夹中复制文件? - How to copy files from folder or subfolder with Python? 如何在目录(python)目录中找到最近修改过的文件夹? - How to find the most recently modified folder in a directory of directories (python)? 如何将CSV文件写入到最近在python中创建的特定文件夹中? - How do you write CSV files to a specific folder that you've recently created in python? 如何将文件复制到 python 中需要访问权限的文件夹? - How to copy files to a folder which requires access permissions in python? 如何在目录中检索最近创建的文件 - How to retrieve the most recently created files in a directory 如何遍历包含 csv 文件的文件夹,提取每个文件的修改日期并使用 Python 创建一个新列作为“修改日期” - How to iterate through a folder that contains csv files, extract each file's modified date and create a new column as " modified date" using Python 使用 python 检查文件夹中所有最后修改的文件的程序? - Program to check all last modified files in a folder using python? 如何为创建的每个项目创建一个带有子文件夹的文件夹? - How to create a folder with subfolder for each project created? 如何在Python目录中的所有子目录中找到最新修改的文​​件? - How to find most recently modified file from all subdirectories in a directory in Python? Python - 如何将模块从另一个文件夹导入子文件夹? - Python - How do you import a module into a subfolder from another folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM