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.
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:
On passing the lines through the pipeline, they are (re-)encoded based on the encoding stored in the $OutputEncoding
preference variable, which defaults to:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With