简体   繁体   English

如何使用2to3就地编辑文件?

[英]How to use 2to3 to edit files in-place?

I am able to load the 2to3 file in Terminal. 我可以在终端中加载2to3文件。 It does a lot of printing, with output like the following: 它进行了大量打印,输出如下:

- print str
+ print(str)  ... 

But the file is left without changes. 但是文件保留不变。 And I don't have any instructions after “run 2to3 ”. 在“运行2to3 ”之后,我没有任何说明。 Please help. 请帮忙。

If you search for “2to3” the first result is the documentation for 2to3 . 如果搜索“ 2to3”,则第一个结果是2to3文档 As the documentation says, to overwrite the file instead of printing a diff, you just need to add the -w flag: 如文档所述,要覆盖文件而不是打印差异,只需添加-w标志:

[When you run the following:] [运行以下命令时:]

 $ 2to3 example.py 

A diff against the original source file is printed. 将打印与原始源文件的差异。 2to3 can also write the needed modifications right back to the source file. 2to3还可以将所需的修改直接写回到源文件。 (A backup of the original file is made unless -n is also given.) Writing the changes back is enabled with the -w flag: (除非也给出-n ,否则将备份原始文件。)使用-w标志启用回写更改:

 $ 2to3 -w example.py 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM