发现linux下写python, 保存后的.py文件默认是没有x权限
这样一来, 即使代码首行添加了
#!/usr/bin/env python
在首次执行之前还要先chmod
请问大家有什么默认赋x权限的办法吗
伊谢尔伦2017-04-18 09:06:25
I rarely write scripts for direct execution, so I haven’t thought about this function. However, there are still a few ideas.
I don’t know what editor the questioner is using, but I usually use vim
,当然下面我说的方法在 emacs
and it works.
Open your vim
配置文件,默认一般是 ~/.vimrc
, so I usually edit it like this:
vim ~/.vimrc
Then add this sentence at the end:
au BufWritePost * if getline(1) =~ "^#!" | silent !chmod a+x <afile>
This sentence means that if the first line of your file starts with #!
, it will be given executable permission by default.
After adding, save and exit, and then you edit a python
文件试试,应该是默认加执行权限了,我在ubuntu
和 debian
and all tests pass.
If you are using emacs
, you may wish to refer to this article!
怪我咯2017-04-18 09:06:25
First of all, the poster’s understanding is somewhat wrong. #!/usr/bin/env python is the interpreter of the specified script, and +x gives execution permissions to the owner, group members and others. There is no connection between the two. Secondly, you can also write a simple script and execute it every time you use vi to open the .py file, but I think it is not necessary. After all, it is just a matter of chmod command.
大家讲道理2017-04-18 09:06:25
pythonxxx.py
Or you can write a script called run
and the content inside is
python $*
Then add execution permission, run xxx.py
You can modify this script
For example, if the suffix is py, execute Python, if it is c, use gcc, if it is .cpp, use g++..
PHPz2017-04-18 09:06:25
"+x" is to increase the executable permissions, and "#!/usr/bin/env python" is which interpreter to use to run your script.
伊谢尔伦2017-04-18 09:06:25
Set umask in your Linux environment and add x to yourself by default, such as setting it to 766