python - 将 .BAT 文件转换为支持 linux 的文件

标签 python linux windows shell batch-file

问题 1:我认为这将转换为 shell 脚本,但我唯一的问题是我需要更改代码中的任何内容吗?这只是简单地运行一个 python 脚本文件。

cd ..
cd Bin\Randomizer
cls
python Randomizer.py

问题 2:当我有 python 脚本时,我是否需要在该计算机上安装 python,以便 python 脚本能够像我知道的那样工作,当我编码时我需要它,但如果我通过电子邮件发送它并且有人启动它,他们需要 python 吗?如果是的话有没有办法避免这种情况?就像当你访问一个网站并玩游戏时,它不会让你安装 python。

这是一个脚本示例

import time
import random 
"""Randomizer Script"""
print("Randomizer Loaded!")
print()
time.sleep(3)
done = 0
while done == 0: 
    name = input("What is your name? ")
    print("Welcome " + name + " To Randomizer!")
    time.sleep(1)
    print("This program will randomize things for a chosen choice!")
    print()
    print("Before we start this program can only allow 6 names at a time")
    time.sleep(3)
    num = input("How Many Things Are You Randomizing? 2 - 6 ")
    def randomizer2():
        name1 = input("What Is the First Persons Name ")
        name2 = input("What Is the Second Persons Name ")
        chosennum = random.randint(1, 2)
        if chosennum == 1: 
            if name1.isalpha():
                chosenname = name1
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 2: 
            if name2.isalpha():
                chosenname = name2
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
    def randomizer3():
        name1 = input("What Is the First Persons Name ")
        name2 = input("What Is the Second Persons Name ")
        name3 = input("What Is the Third Persons Name ")
        chosennum = random.randint(1, 3)
        if chosennum == 1: 
            if name1.isalpha():
                chosenname = name1
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 2: 
            if name2.isalpha():
                chosenname = name2
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 3:
            if name3.isalpha():
                chosenname = name3
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")        
    def randomizer4():
        name1 = input("What Is the First Persons Name ")
        name2 = input("What Is the Second Persons Name ")
        name3 = input("What Is the Third Persons Name ")
        name4 = input("What Is the Fourth Persons Name ")
        chosennum = random.randint(1, 4)
        if chosennum == 1: 
            if name1.isalpha():
                chosenname = name1
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif num == 2: 
            if name2.isalpha():
                chosenname = name2
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif num == 3:
            if name3.isalpha():
                chosenname = name3
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")

        elif chosennum == 4:
            if name4.isalpha():
                chosenname = name4
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
    def randomizer5():
        name1 = input("What Is the First Persons Name ")
        name2 = input("What Is the Second Persons Name ")
        name3 = input("What Is the Third Persons Name ")
        name4 = input("What Is the Fourth Persons Name ")
        name5 = input("What Is the Fifth Persons Name ")
        chosennum = random.randint(1, 5)
        if chosennum == 1: 
            if name1.isalpha():
                chosenname = name1
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 2: 
            if name2.isalpha():
                chosenname = name2
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 3:
            if name3.isalpha():
                chosenname = name3
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")

        elif chosennum == 4:
            if name4.isalpha():
                chosenname = name4
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif chosennum == 5: 
            if name5.isalpha():
                chosenname = name5
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
    def randomizer6():
        name1 = input("What Is the First Persons Name ")
        name2 = input("What Is the Second Persons Name ")
        name3 = input("What Is the Third Persons Name ")
        name4 = input("What Is the Fourth Persons Name ")
        name5 = input("What Is the Fifth Persons Name ")
        name6 = input("What Is the Sixth Persons Name ")
        chosennum = random.randint(1, 6)
        if chosennum == 1: 
            if name1.isalpha():
                chosenname = name1
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif num == 2: 
            if name2.isalpha():
                chosenname = name2
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif num == 3:
            if name3.isalpha():
                chosenname = name3
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")

        elif chosennum == 4:
            if name4.isalpha():
                chosenname = name4
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")
        elif num == 5: 
            if name5.isalpha():
                chosenname = name5
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")

        elif num == 6:
            if name6.isalpha():
                chosenname = name6
                print("The Name "+chosenname+" Has Been Chosen!")
            else:
                print("Do Not Input Numbers Input Letters!")

    if num.isdigit() and num == "2": 
        randomizer2()
    elif num.isdigit() and num == "3":
        randomizer3()
    elif num.isdigit() and num == "4":
        randomizer4()
    elif num.isdigit() and num == "5":
        randomizer5()
    elif num.isdigit() and num == "6":
        randomizer6()
    else:
        print("Please Insert Numbers!")
    stay = input("Do You Want To Randomize Again? Y/N ")
    if stay == "Y":
        done = 0
    elif stay == "N":
        done = 1
        website = "Nothing :("
        print("Thank You For Using Are Programs for more visit " + website)
        time.sleep(7)

最佳答案

坦白说,我不明白你的第一个问题。您显示的代码绝对不会转换任何内容。而且python不是shell脚本。你想让我做什么 ?如果您想执行 python 脚本,请在终端中使用 python script.py

关于第二个问题:

如果您将 python 代码邮寄给某人,他们需要安装 python 解释器才能运行它。解释器需要是正确的 python 版本,不能用 3.x 运行 2.x,反之亦然。他们还需要拥有您正在使用的库。

话虽如此,有一些方法可以打包 python 代码。看这里:How to make a Python script standalone executable to run without ANY dependency?

如果您想通过单击来执行 python 脚本,这 3 个步骤应该可以解决问题:

  1. 将此行添加到脚本顶部:#!/usr/bin/env python 。它应该是第一行代码。
  2. 将文件标记为可执行文件,在终端中运行以下命令chmod +x script.py
  3. 编辑您的文件浏览器首选项。应该有一个设置可以在“运行”和“打开”可执行脚本文件之间切换。

之后只需双击脚本即可。

编辑:

代码的第一行应该是

#!/usr/bin/env python

#!/usr/bin/env python3

取决于代码的 python 版本。

关于python - 将 .BAT 文件转换为支持 linux 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41715528/

相关文章:

python - 标准库中有Python inspect.getcallargs 的逆操作吗?

javascript - plotly PyQt : Get camera view properties

php - openoffice headless 命令文档/引用

linux - 为 Intel Edison 交叉编译 portAudio

regex - Linux:如何仅使用 ls 列出所有文件/目录

windows - 当正文包含回车符和换行符时,如何使用 blat 从命令行发送电子邮件?

python - 无法将对象类型转换为字符串;然后过滤该字符串; python pandas 数据框

Python 数据类型

c - 严格的 C Win GUI 编程

windows - 如何检查我的电脑上是否安装了 gcc