标题: [原创代码] 选择py2.x的文件右键发送给一个批处理来直接转成 3.x -快捷方法 [打印本页]
作者: pendave 时间: 2017-6-10 20:43 标题: 选择py2.x的文件右键发送给一个批处理来直接转成 3.x -快捷方法
写一个批处理
------将python的2.x版本转为3.x.cmd
这个选择一个 py 2.x 的文件后 右键发送给这个批处理来直接转成 3.x 的版本- @echo off
- cls
- set PATH=%PATH%;
- :my_loop
- if "%~1"=="" goto completed
- echo %1
- python "C:\Program Files\Python 3.5\Tools\scripts\2to3.py" -w %1
- SHIFT
- goto my_loop
- :completed
- rem PAUSE
复制代码
放进你
C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\SendTo
文件夹里就可以用
作者: yu2n 时间: 2017-6-17 15:06
本帖最后由 yu2n 于 2017-6-17 15:53 编辑
我這裡 Python 3.5.2 默認安裝位置在:- python -c "import sys; print(sys.executable);"
- C:\Users\Yu2n\AppData\Local\Programs\Python\Python35-32\python.exe
复制代码
所以,重複造個輪子:- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- :: Python2to3.bat
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- @echo off & cls
-
- :::::::::::::::::::::::::::
- :: Check if the system has python installed
- :::::::::::::::::::::::::::
- set "pyver="
- for /f "tokens=1,* delims= " %%a in ('python --version') do (set "pyver=%%~b")
- if not defined pyver (
- echo Please install python 3 version ^!
- goto completed
- )
-
- :::::::::::::::::::::::::::
- :: Check the python version of the system installation
- :::::::::::::::::::::::::::
- echo python version^: %pyver%
- set "pyver=%pyver:.=%"
- set /a "pyver=%pyver:~0,2%"
- if %pyver% lss 30 (
- echo Please use python 3 version ^!
- goto completed
- )
-
- :::::::::::::::::::::::::::
- :: Get the python installation path
- :::::::::::::::::::::::::::
- for /f "delims=" %%a in ('python -c "import sys; print(sys.executable);"') do (set "pydir=%%~dpa")
- echo python directory^: %pydir%
-
- :::::::::::::::::::::::::::
- :my_loop :: Change the python version 2.x to 3.x
- :::::::::::::::::::::::::::
- if "%~1"=="" (
- echo ========================================================================
- echo Done!
- goto completed
- )
- call :bak "%~1"
- echo python 2to3.py -w %1
- echo ========================================================================
- python "%pydir%\Tools\scripts\2to3.py" -w %1
- shift
- goto my_loop
-
- :::::::::::::::::::::::::::
- :bak :: backup file
- :::::::::::::::::::::::::::
- if not defined n set /a n=0
- set /a n+=1
- if exist "%~dp1%~n1 (%n%)%~x1.bak" (
- goto :bak
- ) else (
- echo backup^: "%~1" "%~dp1%~n1 (%n%)%~x1.bak"
- copy "%~1" "%~dp1%~n1 (%n%)%~x1.bak"
- goto :eof
- )
-
- :::::::::::::::::::::::::::
- :completed
- :::::::::::::::::::::::::::
- PAUSE
复制代码
結果如下:- python version: 352
- python directory: C:\Users\Yu2n\AppData\Local\Programs\Python\Python35-32\
- backup: "C:\Users\Yu2n\Desktop\0.py" "C:\Users\Yu2n\Desktop\0 (10).py.bak"
- 1 file(s) copied.
- python 2to3.py -w C:\Users\Yu2n\Desktop\0.py
- ========================================================================
- RefactoringTool: Skipping optional fixer: buffer
- RefactoringTool: Skipping optional fixer: idioms
- RefactoringTool: Skipping optional fixer: set_literal
- RefactoringTool: Skipping optional fixer: ws_comma
- RefactoringTool: No changes to C:\Users\Yu2n\Desktop\0.py
- RefactoringTool: Files that need to be modified:
- RefactoringTool: C:\Users\Yu2n\Desktop\0.py
- ========================================================================
- Done!
- Press any key to continue . . .
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |