我正在使用Selenium Webdriver执行Web测试自动化。我对python和Web测试非常陌生。问题是:我想编写一个Python代码,该代码将通过以下方式自动打开文件:
“ 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
这里AB_103_B7是最新创建或修改的文件夹。每个文件夹包含一个与主文件夹同名的子文件夹(例如:AB_103_B7)和一个ZIP文件夹(AB_103_B7.zip)。子文件夹和ZIP文件夹包含3个具有不同扩展名的文件(例如.bin,.hex和.hex)
我想访问最新的修改或创建的文件夹,然后自动从此子文件夹上载文件,还将zip文件夹上载到网站。
注意:主文件夹“ AB_ * ”不断更新。因此,代码必须通过名称或修改时间来检测最新文件夹。我可以通过设置目录直接上传文件,例如:driver.find_element_by_id('abc').send_keys("C:\software\AB_103_B7\AB_103_B7\test.bin")
。但是我的问题是访问最新文件夹。
有什么可以帮助我进行python编码吗?我检查os.walk()
了其他问题。我对应该为dirname,subdirs,files编写的内容感到困惑。
尝试使用此代码,如有任何问题,请通知我:
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_))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句