how to make a python or perl script portable to both linux and windows?

前端 未结 4 2200
再見小時候
再見小時候 2021-02-14 04:26

I was wondering how to make a python script portable to both linux and windows?

One problem I see is shebang. How to write the shebang so that the script can be run on

4条回答
  •  时光说笑
    2021-02-14 05:11

    I don't know enough to comment on Python approaches to this problem, so I won't.

    Most things in Perl will just work. There are a few gotchas that are easy to avoid.

    Here are a few things I have come across in the years I've been working with Win32 Perl:

    • Use 3 argument form of open. The two argument form can have problems with spaces in paths. (You should already be doing this anyway.)
    • Make sure that the case is correct when you use a module. use Warnings; will appear to work, but in reality it will fail.
    • select only works on actual sockets under Windows. You can't use it on any other sort of handle.
    • Use File::Spec to manage paths to files.
    • When you open a file handle CRLF will automatically be converted to LF line endings as the handle is read. LF is changed to CRLF on write. If you want to avoid this, use binmode on the handle to prevent the translation.
    • If you need to pass arguments through a shell, put double quotes around each argument. This will prevent errors due to spaces in file names.

    See perlport for more information on individual functions.

提交回复
热议问题