0

So I was doing a batch file to make a python file but I noticed that when doin echo if age >= 18: >> example.py it would only write in the py file "if age" because of the character ">". What do you think it's the best solution?

  • 1
    why do you generate python code in a batch file? – balderman Feb 22 '21 at 16:23
  • 1
    Would you mind adding more explanation to your question so as to help us understand what you're trying to achieve. Also, add what you've tried so far. – OGx09 Feb 22 '21 at 16:24
  • 1
    You need to escape the problematic character with a caret, `^`. Example: `@(Echo if age ^>= 18:) 1>> "example.py"` Additionally your question has been asked, and answered multiple times before on this site, please use the search facility next time. – Compo Feb 22 '21 at 16:26
  • I want to generate it in batch just for fun really but I could do it normally to be honest. I'm starting learning python so I thought it would be fun to explore every way to do it – Toho Seiwa Feb 22 '21 at 16:29
  • thanks. I really appreciate it – Toho Seiwa Feb 22 '21 at 16:30

1 Answers1

0

You need to escape the > character. In Batch, characters are escaped using the ^ character, so the statement will look like this:

ECHO if age ^>= 18: >> example.py

Howeer, you may soon discover an issue whereby leading whitespace (which is important to Python) is stripped when echoing from batch:

ECHO if age ^>= 18: >> example.py
ECHO     print("adult!") >> example.py

will produce

if age >= 18:
print("adult!")

which will not work. You need to use ECHO. to preserve leading whitespace:

ECHO.if age ^>= 18:
ECHO.    print("adult!")

which will produce:

if age >= 18:
   print("adult!")
Andrew Ray
  • 1,815
  • 10
  • 20
  • Your example does not technically do what was intended, as it will include trailing whitespace on each of the `echo`ed lines. Your second example, should read, **line 1**: `@(ECHO if age ^>= 18:` and **line 2**: `ECHO print("adult!"^)) 1> "example.py"`. This structure works best because it only opens the file once for writing. (Please note that the nested closing parentheses also require escaping when using this particular structure too!) – Compo Feb 22 '21 at 16:38
  • @Compo I didn't want to get into the deep esoterica of batch. OP did not seem concerned with efficiency, and Python ignores training spaces, so I didn't worry about these things. – Andrew Ray Feb 22 '21 at 16:42
  • Perhaps not, but as you were already answering a question which is duplicated all over this site, your answer would be better, if it actually provided something more than just including the required escape character. Also, you were also incorrect with your result from your second code snippet. The leading spaces, _(between `ECHO` and `print("adult!")`)_, will not be removed at all, and will work just fine. Also just to be clear, to `Echo` an empty line, _(i.e. CRLF)_, `.` is not the best character to use after `ECHO` either, `(`, `=`, and `/` are each more robust. – Compo Feb 22 '21 at 17:15