转换.BAT文件到linux支持

时间:2017-01-18 09:23:46

标签: python linux windows shell batch-file

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

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

问题2:当我有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)

1 个答案:

答案 0 :(得分:2)

坦率地说,我不明白你的第一个问题。您展示的代码肯定不会转换任何内容。 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脚本,这三个步骤应该可以解决问题:

  1. 将此行添加到脚本顶部:#!/usr/bin/env python。它应该是第一行代码。
  2. 将文件标记为可执行文件,在终端chmod +x script.py
  3. 中运行以下命令
  4. 编辑文件资源管理器首选项。应该有一个在"运行"之间切换的设置。和"开放"可执行脚本文件。
  5. 之后只需双击脚本。

    修改

    代码的第一行应该是

    #!/usr/bin/env python
    

    #!/usr/bin/env python3
    

    取决于代码的python版本。