-2

I am attempting to run this .PY file from Command Prompt:

# Merge two .BSG files
# Starting block and world position are taken from the first file
# Example: "bsgmerge input.bsg output.bsg merged.bsg"

import io, sys

one = open(sys.argv[1]).readlines()
two = open(sys.argv[2]).readlines()

for n in [1,3,5,7,9,11,17,19,21,23]:
    one[n] = one[n][:-1]+"|"+two[n].partition("|")[2]

open(sys.argv[3],"w").write("".join(one))

It is a program that takes a creation from the game Beseige and merges it with another saved creation so that opening the merged file results in both creations being present. If you want more details, you can read up on that here.

I am having trouble figuring out how to call this program from the command line. At first I thought the problem was me having Python 2 (it requires Python 3), so I uninstalled 2 and installed 3. This did not help.

What I am doing is entering the "python" command to pull up the Python environment within CMD, then entering the command to call the program based on the third comment in the file ("bsgmerge input.bsg output.bsg merged.bsg").

I tried using full file paths or simply changing to the correct directory before typing the "python" command and using only the file names, but so far I've had no luck.

When I am in the correct directory, then enter the Python environment, typing the command "bsgmerge 1.bsg 2.bsg M.bsg" (my existing files to be merged are 1.bsg and 2.bsg), this error occurs:

File "<stdin>", line 1
  bsgmerge 1.bsg 2.bsg M.bsg
            ^

SyntaxError: invalid syntax

I took a Python course (which is why I used to have Python 2 on my machine) last fall, so I noticed that there is no "def" defining a function in the above code, which is something I've never encountered, so I'm thinking that is the root of my problems.

Thanks in advance for the help.

Zach Brown
  • 53
  • 1
  • 1
  • 4
  • 1
    `python myscript.py`? – Mephy Jul 31 '16 at 18:17
  • @Mephy's answer will work, while something simpler might be `./bsgmerge.py bsg 2.bsg`. For that all you need is a [shebang](http://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take). – David Gomes Jul 31 '16 at 18:19
  • @DavidGomes that's true for *nix systems. AFAIK Windows do not handle text files as executables, and the tag "cmd" applies only to the Windows' shell. – Mephy Jul 31 '16 at 18:28
  • @Mephy worked for me! My file is named bsgmerge.py and the command "python bsgmerge.py 1.bsg 2.bsg M.bsg" worked perfectly. Sorry that this was such a rookie question but my Python class had me stuck using JES (ugh) for so long that I barely learned the command line stuff. – Zach Brown Jul 31 '16 at 18:32
  • For maximum flexibility, use `venv` that comes included in Python 3. – lit Aug 01 '16 at 19:25

1 Answers1

0

I was probably same problem with python launcher.

If you use Linux, first line shoud be:

#! /path/to/your/python/3

In Windows it some more complicated:

In registry by regedit change HKEY_CLASSES_ROOT\Python.File\shell\open\command from "C:\Python27\python.exe" "%1" %* to "C:\Windows\py.exe" "%1" %*.

And first line of script shoud be:

#! python3

Now it shoud work properly.

David Gomes
  • 5,644
  • 16
  • 60
  • 103
Egor Egorov
  • 313
  • 1
  • 4
  • 19