[英]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.