Python script sunflower one-key batch copy files

 

problem

QA Xiao Zhangxin took a test task, one of which requires the use of Android devices with batches of videos or pictures, and also requires the use of different models and system adaptations. If you use traditional manual copy operations, the cost is very high. First To find the system album and then copy it

  • There are few photos or videos in the album, so you need to perform multiple select copy operations in the album multiple times

  • There are few photos or videos in the album. If importing from the outside, make sure that there are batch files outside and understand the path to be imported

  • If it involves adaptation, it needs to be operated once on each device

problem analysis

 

Is there a solution that can automatically copy files?

Associated with the adb shell cp command supports file copying, you can consider automating the packaging script. In addition, before copying, you need to check the adb connection status, and you need to refresh the gallery after copying, otherwise the system album cannot update the copied files in time

Program Advantage Disadvantage
Scheme one manual operation Direct operation, no time-consuming scripting Repeated operations are time-consuming
Scheme two automation script Develop once and use many times Need to download and be familiar with the script


Python script implementation

Step 1: Check the adb connection status

 def check_adb_status(self):
print("第一步:检查adb连接状态")
command = 'adb devices'
adbfile = os.popen(command, "r")
result = adbfile.read()
adbfile.close()
# 对result处理,获取连接状态
formatresult = result.replace('\n', '').replace(' ', '')
if " device" not in formatresult:
print("adb连接失败,请重新连接\n")
sys.exit(1)
else:
print("adb连接成功\n")
self.get_lock_screen()


Step two realizes the function of copying. Two parameters file_path and copy_count are passed in, representing the source file path to be copied and the number of times to be copied.

 def run_copy_file(cls, file_path, copy_count):
ADB_PUSH = 'adb push %s /data/local/tmp/' % file_path
os.system(ADB_PUSH)
time.sleep(2)
filename = os.path.basename(file_path)
print('filename名称为' + filename)
now = datetime.datetime.now()
nowtime = now.strftime('%Y_%m_%d_%H_%M_%S')
count = 1
while count <= copy_count:
ADB_SHELL_CP = 'adb shell cp /data/local/tmp/%s /sdcard/DCIM/Camera/%s_%s_%s'\
% (filename, count, nowtime, filename)
print("第%s次复制" % count)
print("ADB_SHELL_CP*******" + ADB_SHELL_CP)
os.system(ADB_SHELL_CP)
time.sleep(2)
count = count + 1

#更新广播,刷新系统相册
print("复制完成,刷新相册")
os.system(ADB_SHLEL_BROADCAST)

Note: In order to identify the copied file, the file name is composed of three parts, the number of copies_current time_the name of the copied file, such as 29_2020_04_19_19_11_50_jiantou.gif script execution process and results

Pass in the local file path to be copied and the expected number of times of copying. Take the copying of gif file and the number of times of copying as 30 times for example, as follows

if __name__ == "__main__":
cpfile = CopyFile()
cpfile.check_adb_status()
copy_file_count = 30
# file_path = r"D:\技术\读书笔记\gaoxiaoxuexi.jpg"
copy_file_path = r"D:\技术\读书笔记\jiantou.gif"
# file_path = r'D:\技术\读书笔记\WeChat_20200409154818.mp4'
CopyFile.run_copy_file(copy_file_path, copy_file_count)

Script execution results, screenshots are as follows

第一步:检查adb连接状态
adb连接成功

第二步:开始检查设备锁屏状态
未锁屏可继续操作

441 KB/s (15827 bytes in 0.035s)
filename名称为jiantou.gif
第1次复制
ADB_SHELL_CP*******adb shell cp /data/local/tmp/jiantou.gif /sdcard/DCIM/Camera/1_2020_04_19_19_11_50_jiantou.gif
....
第29次复制
ADB_SHELL_CP*******adb shell cp /data/local/tmp/jiantou.gif /sdcard/DCIM/Camera/29_2020_04_19_19_11_50_jiantou.gif
第30次复制
ADB_SHELL_CP*******adb shell cp /data/local/tmp/jiantou.gif /sdcard/DCIM/Camera/30_2020_04_19_19_11_50_jiantou.gif
复制完成,刷新相册
Broadcasting: Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE dat=file:///sdcard/DCIM/Camera flg=0x400000 }
Broadcast completed: result=0

After the script is executed, the copied files are displayed under the device camera and device files, as shown below

 

 

 

 

 


Script format check

Check whether the code needs to meet the specifications through pylint, the following check results 7.45 / 10 need to be optimized

pylint copyfile_nosh.py
************* Module 9.utils.copyfile_nosh
copyfile_nosh.py:1:0: C0114: Missing module docstring (missing-module-docstring)
copyfile_nosh.py:17:4: C0116: Missing function or method docstring (missing-function-docstring)
copyfile_nosh.py:32:4: C0116: Missing function or method docstring (missing-function-docstring)
copyfile_nosh.py:32:4: R0201: Method could be a function (no-self-use)
copyfile_nosh.py:45:4: C0116: Missing function or method docstring (missing-function-docstring)
copyfile_nosh.py:45:27: W0621: Redefining name 'file_path' from outer scope (line 71) (redefined-outer-name)
copyfile_nosh.py:45:38: W0621: Redefining name 'copy_count' from outer scope (line 69) (redefined-outer-name)
copyfile_nosh.py:46:8: C0103: Variable name "ADB_PUSH" doesn't conform to snake_case naming style (invalid-name)
copyfile_nosh.py:53:12: C0103: Variable name "ADB_SHELL_CP" doesn't conform to snake_case naming style (invalid-name)
copyfile_nosh.py:45:4: R0201: Method could be a function (no-self-use)
copyfile_nosh.py:67:4: C0103: Constant name "cpfile" doesn't conform to UPPER_CASE naming style (invalid-name)
copyfile_nosh.py:69:4: C0103: Constant name "copy_count" doesn't conform to UPPER_CASE naming style (invalid-name)
copyfile_nosh.py:71:4: C0103: Constant name "file_path" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 7.45/10 (previous run: 6.20/10, +1.25)

After the modification according to the prompt, the execution result is as follows, and finally more than 9 points. If it is associated with git and sets git-pylint configuration limit = 9.0, the code can be submitted successfully.

Note: If you want to ignore some prompts, you can use -d as follows

pylint copyfile_nosh.py -d C0103 -d C0116
************* Module 9.utils.copyfile_nosh
copyfile_nosh.py:1:0: C0114: Missing module docstring (missing-module-docstring)

------------------------------------------------------------------
Your code has been rated at 9.81/10 (previous run: 9.81/10, +0.00)


Encountered pit

After the file is copied, the system photo album still does not display the copied photos or videos. At this time, you need to refresh the gallery and execute the following command

Solution one can execute the following command

adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/DCIM/Camera

Option two restarts the device, if option one does not take effect

you may also like

 

 

Guess you like

Origin www.cnblogs.com/echoqi/p/12734086.html