[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程批处理在线视频分享
返回列表 发帖

[原创代码] 选择py2.x的文件右键发送给一个批处理来直接转成 3.x -快捷方法

写一个批处理
------将python的2.x版本转为3.x.cmd

这个选择一个 py 2.x 的文件后 右键发送给这个批处理来直接转成 3.x 的版本
  1. @echo off
  2. cls
  3. set PATH=%PATH%;
  4. :my_loop
  5. if "%~1"=="" goto completed
  6. echo %1
  7. python "C:\Program Files\Python 3.5\Tools\scripts\2to3.py" -w %1
  8. SHIFT
  9. goto my_loop
  10. :completed
  11. rem PAUSE
复制代码
放进你
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\SendTo

文件夹里就可以用

附件: 您需要登录才可以下载或查看附件。没有帐号?注册
1

评分人数

本帖最后由 yu2n 于 2017-6-17 15:53 编辑

我這裡 Python 3.5.2 默認安裝位置在:
  1. python -c "import sys; print(sys.executable);"
  2. C:\Users\Yu2n\AppData\Local\Programs\Python\Python35-32\python.exe
复制代码
所以,重複造個輪子:
  1. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. :: Python2to3.bat
  3. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  4. @echo off & cls
  5. :::::::::::::::::::::::::::
  6. :: Check if the system has python installed
  7. :::::::::::::::::::::::::::
  8. set "pyver="
  9. for /f "tokens=1,* delims= " %%a in ('python --version') do (set "pyver=%%~b")
  10. if not defined pyver (
  11.   echo Please install python 3 version ^!
  12.   goto completed
  13. )
  14. :::::::::::::::::::::::::::
  15. :: Check the python version of the system installation
  16. :::::::::::::::::::::::::::
  17. echo python version^: %pyver%
  18. set "pyver=%pyver:.=%"
  19. set /a "pyver=%pyver:~0,2%"
  20. if %pyver% lss 30 (
  21.   echo Please use python 3 version ^!
  22.   goto completed
  23. )
  24. :::::::::::::::::::::::::::
  25. :: Get the python installation path
  26. :::::::::::::::::::::::::::
  27. for /f "delims=" %%a in ('python -c "import sys; print(sys.executable);"') do (set "pydir=%%~dpa")
  28. echo python directory^: %pydir%
  29. :::::::::::::::::::::::::::
  30. :my_loop                 :: Change the python version 2.x to 3.x
  31. :::::::::::::::::::::::::::
  32.   if "%~1"=="" (
  33.     echo ========================================================================
  34.     echo Done!
  35.     goto completed
  36.   )
  37.   call :bak "%~1"
  38.   echo python 2to3.py -w %1
  39.   echo ========================================================================
  40.   python "%pydir%\Tools\scripts\2to3.py" -w %1
  41.   shift
  42.   goto my_loop
  43. :::::::::::::::::::::::::::
  44. :bak                     :: backup file
  45. :::::::::::::::::::::::::::
  46.   if not defined n set /a n=0
  47.   set /a n+=1
  48.   if exist "%~dp1%~n1 (%n%)%~x1.bak" (
  49.     goto :bak
  50.   ) else (
  51.     echo backup^: "%~1" "%~dp1%~n1 (%n%)%~x1.bak"
  52.     copy "%~1" "%~dp1%~n1 (%n%)%~x1.bak"
  53.     goto :eof
  54.   )
  55. :::::::::::::::::::::::::::
  56. :completed
  57. :::::::::::::::::::::::::::
  58.   PAUSE
复制代码
結果如下:
  1. python version: 352
  2. python directory: C:\Users\Yu2n\AppData\Local\Programs\Python\Python35-32\
  3. backup: "C:\Users\Yu2n\Desktop\0.py" "C:\Users\Yu2n\Desktop\0 (10).py.bak"
  4.         1 file(s) copied.
  5. python 2to3.py -w C:\Users\Yu2n\Desktop\0.py
  6. ========================================================================
  7. RefactoringTool: Skipping optional fixer: buffer
  8. RefactoringTool: Skipping optional fixer: idioms
  9. RefactoringTool: Skipping optional fixer: set_literal
  10. RefactoringTool: Skipping optional fixer: ws_comma
  11. RefactoringTool: No changes to C:\Users\Yu2n\Desktop\0.py
  12. RefactoringTool: Files that need to be modified:
  13. RefactoringTool: C:\Users\Yu2n\Desktop\0.py
  14. ========================================================================
  15. Done!
  16. Press any key to continue . . .
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表