5

I'm having problems reading text files into my python programs.

import sys

words = sys.stdin.readlines()

I'm reading the file in through stdin but when I try to execute the program I'm getting this error.

PS> python evil_61.py < evilwords.txt

At line:1 char:19
+ python evil_61.py < evilwords.txt
+                   ~

The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

Could someone tell me how to run these kinds of programs as it is essential for my course and I'd rather use Windows than Linux.

mklement0
  • 312,089
  • 56
  • 508
  • 622

3 Answers3

4

Since < for input redirection is not supported in PowerShell, use Get-Content in a pipeline instead:

Get-Content evilwords.txt | python evil_61.py 

Note: Adding the -Raw switch - which reads a file as a single, multi-line string - would speed things up in principle (at the expense of increased memory consumption), but PowerShell invariably appends a newline to data piped to external programs, as of PowerShell 7.2 (see this answer), so the target program will typically see an extra, empty line at the end. Get-Content's default behavior of line-by-line streaming avoids that.

Beware character-encoding issues:

  • Get-Content, in the absence of an -Encoding argument, assumes the following encoding:

    • Windows PowerShell (the built-into-Windows edition whose latest and final version is 5.1): the active ANSI code page, which is implied by the active legacy system locale (language for non-Unicode programs).
    • PowerShell (Core) 7+: (BOM-less) UTF-8
  • On passing the lines through the pipeline, they are (re-)encoded based on the encoding stored in the $OutputEncoding preference variable, which defaults to:

    • Windows PowerShell: ASCII(!)
    • PowerShell (Core) 7+: (BOM-less) UTF-8

As you can see, only PowerShell (Core) 7+ exhibits consistent behavior, though, unfortunately, as of PowerShell Core 7.2.0-preview.9, this doesn't yet extend to capturing output from external programs, because the encoding that controls the interpretation of received data, stored in [Console]::OutputEncoding], still defaults to the system's active OEM code page - see GitHub issue #7233.

mklement0
  • 312,089
  • 56
  • 508
  • 622
  • @JohnHubert, having just revisited this answer, I've updated it to include a specific explanation for why `-Raw` is problematic when piping to external programs, specifically. (I've also added character-encoding information.) – mklement0 Sep 24 '21 at 15:36
0

Consider passing the text file as an argument at command line for Python script to use. All command line arguments (including file name) come stored in the sys.argv list:

Python Script (in evil_61.py)

import sys

txtfile = sys.argv[1]
with open(txtfile) as f:
    content = f.readlines()

PowerShell Command

PS> python evil_61.py evilwords.txt
mklement0
  • 312,089
  • 56
  • 508
  • 622
Parfait
  • 97,543
  • 17
  • 91
  • 116
-2

Because the '<' is interpreted by the PowerShell engine as redirection operator.

Quit PowerShell for your Python stuff and hop on to cmd or cygwin. Based on the context of this question, I see no reason why you should use escape characters for the sake of using PowerShell as CLI to learn Python.

M.M.
  • 52
  • 3
  • `>` is _not_ being interpreted as a redirection operator: it is being interpreted as "reserved for future use" - no more and no less. If it _had_ been interpreted as a redirection operator in the traditional sense, things would have worked as expected. Escape characters don't come into play here, because no escape character can make ` – mklement0 Mar 13 '16 at 04:28