How does a user input a filename?
I’ve written code for an assembler, but I am still new to python.
In my code I have the user input a file that will be converted into an assembly language. I think I’ve almost got it working, but I can’t figure out where the user enters the file name.
I’m in (what I think is) IDLE, and then when I hit F5 it runs in the shell. I’m getting an error, but I’m pretty sure it’s because no file name has been entered. Where is the user supposed to input these kinds of things? Is this done from the python shell, or from the command line, do I need to turn it into an executable? Can someone help clarify where the user is inputting all this information? I’ll put in a segment of code, although I don’t think it’s necessary to answer my questions, but maybe it’ll give you a better idea of my issue.
if __name__ == '__main__': import sys if len(sys.argv) == 1: print 'need filename' sys.exit(-1) table = SymbolTable() parser = Parser(sys.argv[1]) parser.advance() line = 0 while parser.hasMoreCommands(): if parser.commandType() == 'L_COMMAND': table.addEntry(parser.symbol(), line) else: line += 1 parser.advance() code = Code() parser = Parser(sys.argv[1]) parser.advance() var_stack = 16 while parser.hasMoreCommands(): cmd_type = parser.commandType() if cmd_type == 'A_COMMAND': number = 32768 try: addr = int(parser.symbol()) except: if table.contains(parser.symbol()): addr = table.getAddress(parser.symbol()) else: table.addEntry(parser.symbol(), var_stack) addr = var_stack var_stack += 1 bin_number = bin(number | addr)[3:] assembly = '0' + bin_number print assembly elif cmd_type == 'C_COMMAND': assembly = '111' assembly += code.comp(parser.comp()) assembly += code.dest(parser.dest()) assembly += code.jump(parser.jump()) print assembly parser.advance()
The part to note is at the beginning lines 4-6 where it’s checking the file name. So once I run my program I get ‘need filename’ printed to the screen and an error message that looks like this:
Traceback (most recent call last): File "C:\Python27\Assembler.py", line 98, in sys.exit(-1) SystemExit: -1
fileinput — Iterate over lines from multiple input streams¶
This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see open() .
import fileinput for line in fileinput.input(encoding="utf-8"): process(line)
This iterates over the lines of all files listed in sys.argv[1:] , defaulting to sys.stdin if the list is empty. If a filename is ‘-‘ , it is also replaced by sys.stdin and the optional arguments mode and openhook are ignored. To specify an alternative list of filenames, pass it as the first argument to input() . A single file name is also allowed.
All files are opened in text mode by default, but you can override this by specifying the mode parameter in the call to input() or FileInput . If an I/O error occurs during opening or reading a file, OSError is raised.
Changed in version 3.3: IOError used to be raised; it is now an alias of OSError .
If sys.stdin is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been explicitly reset (e.g. using sys.stdin.seek(0) ).
Empty files are opened and immediately closed; the only time their presence in the list of filenames is noticeable at all is when the last file opened is empty.
Lines are returned with any newlines intact, which means that the last line in a file may not have one.
You can control how files are opened by providing an opening hook via the openhook parameter to fileinput.input() or FileInput() . The hook must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. If encoding and/or errors are specified, they will be passed to the hook as additional keyword arguments. This module provides a hook_compressed() to support compressed files.
The following function is the primary interface of this module:
fileinput. input ( files = None , inplace = False , backup = » , * , mode = ‘r’ , openhook = None , encoding = None , errors = None ) ¶
Create an instance of the FileInput class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this function will be passed along to the constructor of the FileInput class.
The FileInput instance can be used as a context manager in the with statement. In this example, input is closed after the with statement is exited, even if an exception occurs:
with fileinput.input(files=('spam.txt', 'eggs.txt'), encoding="utf-8") as f: for line in f: process(line)
Changed in version 3.2: Can be used as a context manager.
Changed in version 3.8: The keyword parameters mode and openhook are now keyword-only.
Changed in version 3.10: The keyword-only parameter encoding and errors are added.
The following functions use the global state created by fileinput.input() ; if there is no active state, RuntimeError is raised.
Return the name of the file currently being read. Before the first line has been read, returns None .
Return the integer “file descriptor” for the current file. When no file is opened (before the first line and between files), returns -1 .
Return the cumulative line number of the line that has just been read. Before the first line has been read, returns 0 . After the last line of the last file has been read, returns the line number of that line.
Return the line number in the current file. Before the first line has been read, returns 0 . After the last line of the last file has been read, returns the line number of that line within the file.
Return True if the line just read is the first line of its file, otherwise return False .
Return True if the last line was read from sys.stdin , otherwise return False .
Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect.
The class which implements the sequence behavior provided by the module is available for subclassing as well:
class fileinput. FileInput ( files = None , inplace = False , backup = » , * , mode = ‘r’ , openhook = None , encoding = None , errors = None ) ¶
Class FileInput is the implementation; its methods filename() , fileno() , lineno() , filelineno() , isfirstline() , isstdin() , nextfile() and close() correspond to the functions of the same name in the module. In addition it is iterable and has a readline() method which returns the next input line. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed.
With mode you can specify which file mode will be passed to open() . It must be one of ‘r’ and ‘rb’ .
The openhook, when given, must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. You cannot use inplace and openhook together.
You can specify encoding and errors that is passed to open() or openhook.
A FileInput instance can be used as a context manager in the with statement. In this example, input is closed after the with statement is exited, even if an exception occurs:
with FileInput(files=('spam.txt', 'eggs.txt')) as input: process(input)
Changed in version 3.2: Can be used as a context manager.
Changed in version 3.8: The keyword parameter mode and openhook are now keyword-only.
Changed in version 3.10: The keyword-only parameter encoding and errors are added.
Changed in version 3.11: The ‘rU’ and ‘U’ modes and the __getitem__() method have been removed.
Optional in-place filtering: if the keyword argument inplace=True is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup=’.’ ), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is ‘.bak’ and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.
The two following opening hooks are provided by this module:
fileinput. hook_compressed ( filename , mode , * , encoding = None , errors = None ) ¶
Transparently opens files compressed with gzip and bzip2 (recognized by the extensions ‘.gz’ and ‘.bz2’ ) using the gzip and bz2 modules. If the filename extension is not ‘.gz’ or ‘.bz2’ , the file is opened normally (ie, using open() without any decompression).
The encoding and errors values are passed to io.TextIOWrapper for compressed files and open for normal files.
Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_compressed, encoding=»utf-8″)
Changed in version 3.10: The keyword-only parameter encoding and errors are added.
Returns a hook which opens each file with open() , using the given encoding and errors to read the file.
Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_encoded(«utf-8», «surrogateescape»))
Changed in version 3.6: Added the optional errors parameter.
Deprecated since version 3.10: This function is deprecated since fileinput.input() and FileInput now have encoding and errors parameters.
Create and pipe a file-like object as input for a command
In this example I’m creating a file and passing it as input to cat (in reality it’s not cat I’m running but some other program that parses an input pcap file). What I’m wondering is, is there a way in Python I can create a ‘file-like object’ with some content, and pipe this file-like object as input to a command-line program. If it is possible, I reckon it would be more efficient than writing a file to the disk and then deleting that file.
If the recipient program is not configured to read from STDIN, then you don’t have a choice but to write to disk; otherwise you could stream the data in by simple redirection — assuming I understood what you are trying to do.
@BurhanKhalid: there are named pipes and /dev/fd/N filenames (fdescfs) that allow you to pass the input as a filename without writing the corresponding content to disk.
4 Answers 4
check_output takes a stdin input argument to specify a file-like object to connect to the process’s standard input.
with open('temp.file') as input: out = subprocess.check_output(['cat'], stdin=input)
Also, there’s no need to shell out to run rm ; you can remove the file directly from Python:
you can pass a file-like object only if .fileno() returns a valid file descriptor e.g., you can’t pass StringIO object.
You can write to a TemporaryFile
import subprocess from tempfile import TemporaryFile f = TemporaryFile("w") f.write("foo") f.seek(0) out = subprocess.check_output(['cat'],stdin=f) print(out) b'foo'
If you just want to write to a file like object and get the content:
from io import StringIO f = StringIO() f.write("foo") print(f.getvalue())
If the program is configured to read from stdin , you can use Popen.communicate:
>>> from subprocess import Popen, PIPE >>> p = Popen('cat', stdout=PIPE, stdin=PIPE, stderr=PIPE) >>> out, err = p.communicate(input=b"Hello world!") >>> out 'Hello world!'
If the command accepts only filenames, if it doesn’t read input from its stdin i.e., if you can’t use stdin=PIPE + .communicate() or stdin=real_file then you could try /dev/fd/# filenames:
#!/usr/bin/env python3 import os import subprocess import threading def pump_input(pipe): with pipe: for i in range(3): print(i, file=pipe) r, w = os.pipe() try: threading.Thread(target=pump_input, args=[open(w, 'w')]).start() out = subprocess.check_output(['cat', '/dev/fd/'+str(r)], pass_fds=[r]) finally: os.close(r) print('got:', out)
No content touches the disk. The input is passed to the subprocess via the pipe directly.
If you have a file-like object that is not a real file (otherwise, just pass its name as the command-line argument) then pump_input() could look like:
import shutil def pump_input(pipe): with pipe: shutil.copyfileobj(file_like_object, pipe)