我正在使用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 编写的内容感到困惑。
答案 0 :(得分:0)
尝试此代码,如有任何问题,请告知我们:
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_))