-3

I am Learning Python the Hard Way. How could I edit this code to work with a file named 'bobsDetails.txt'?

from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sajid
  • 39
  • 1
  • 8
  • 2
    Fix *what*?! What's the problem? What happens, and what should happen instead? What is `bobsDetails.txt` supposed to be? – jonrsharpe Sep 22 '14 at 19:18
  • What needs fixing exactly? – shx2 Sep 22 '14 at 19:18
  • A window opens saying"Invalid syntax" – Sajid Sep 22 '14 at 19:20
  • and bobDetails.txt just contains some contact details – Sajid Sep 22 '14 at 19:21
  • It is an object. The file is already made and saved. – Sajid Sep 22 '14 at 19:22
  • So would I need to change the: print bobsDetails.read() to print bobsDetails.txt() ? – Sajid Sep 22 '14 at 19:23
  • from sys import argv script, filename = argv txt = open(filename) print "Here's your file %r:" % filename print txt.read() print "Type the filename again:" file_again = raw_input("> ") txt_again = open(file_again) print txt_again.read() – Sajid Sep 22 '14 at 19:27
  • @user3650299 What is it that you think your second line achieves? – quamrana Sep 22 '14 at 19:28
  • Guys, please read the update – Sajid Sep 22 '14 at 19:30
  • Take a look at [fileinput](https://docs.python.org/2/library/fileinput.html) – dawg Sep 22 '14 at 19:32
  • You have still failed to explain **what the actual problem is**. What does your code do? What should it do? Why do you think it's won't *"work with"* your file? – jonrsharpe Sep 22 '14 at 19:33
  • It should desplay something like this: $ python ex15.py ex15_sample.txt Here's your file 'ex15_sample.txt': This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. – Sajid Sep 22 '14 at 19:34
  • But instead, invalid syntax comes up – Sajid Sep 22 '14 at 19:34
  • So **provide the full error traceback**. Are you [using Python 3.x](http://stackoverflow.com/q/826948/3001761), by any chance? – jonrsharpe Sep 22 '14 at 19:36
  • Sorry, I think I found my mistake. Instead of writing the files name, Im ment to keep it as the example and run it from the command line.You don't put the names of files in, you let Python put the name in – Sajid Sep 22 '14 at 19:39
  • 1
    Seriously, there are only [three steps](http://learnpythonthehardway.org/book/intro.html) (note emphasis on *"exactly"*) - **follow them**. – jonrsharpe Sep 22 '14 at 19:41

2 Answers2

0

You want to look at fileinput

Suppose I have this file:

$ cat bobsDetail.txt
File 'Bob's Detail'

I can write a simple loop that will either 1) process stdin or 2) open the file name and process the contents:

#!/usr/bin/python

import fileinput
for line in fileinput.input():
    print line

Now make that executable (Unix):

$ chmod +x fi.py

Then you can process that any way it is presented:

$ ./fi.py bobsDetail.txt
File 'Bob's Detail'

Or,

$ cat bobsDetail.txt | ./fi.py
File 'Bob's Detail'

Then you can identify stdin vs file:

for line in fileinput.input():
    if fileinput.isfirstline():
        if fileinput.isstdin():
            print 'stdin'
        else:
            print fileinput.filename()    

    print line

From a file

$ ./fi.py bobsDetail.txt
bobsDetail.txt
File 'Bob's Detail'

From stdin:

$ cat bobsDetail.txt | ./fi.py
stdin
File 'Bob's Detail'
dawg
  • 98,345
  • 23
  • 131
  • 206
0

First you need to understand the code before you can change its file name to bobsDetail.txt. If you understand the code your question will becomes very easy. The code does the same procedure of opening and reading the contents of a file twice, first using the argv (argument variable) and then using raw_input(). Perhaps, the book where the code comes from wants to show you how to read the contents of a file using two ways - argv and raw_input(). Paste the code in notepad++ and save it as txtcode.py. Create a text file, type in:

I am studying how to read files using argv and raw_input

Save this text file as OpenMe.txt and save it in the same folder with the python script textcode.py. What the program does is to use the textcode.py script to open and read the contents of OpenMe.txt. In other words, the script in textcode.py is used to open the contents

I am studying how to read files using argv and raw_input

found in the file OpenMe.txt. To run the program open powershell (I am assuming you are using Windows). Change the directory to where you saved your textcode.py and OpenMe.txt files and run the script as:

*python textcode.py OpenMe.txt*
What you need to type and what will be displayed is shown below:
*$ python ex15.py OpenMe.txt
Here's your file 'OpenMe.txt':
I am studying how to read files using argv and raw_input.
Type the filename again:
>  OpenMe.txt
I am studying how to read files using argv and raw_input*

Once you understand this then you can simply change the file named OpenMe.txt into your preferred file bobsDetail.txt To display your file must have some contents, otherwise it will come out as empty and you might think the program is not working. Note, you need to understand how to run the program and I have used the simplest and most efficient means to learn how to program - using a plain text editor like notepad++ and Powershell (or simply the cmd).

chibole
  • 942
  • 6
  • 15