-1

I've got a problem that has been troubling me for nearly a week. It was an exercise given out in class (not as homework) for us to try and find the answer to. The question is:

"Write a program that prompts the user to enter the name of a file, and then determines whether or not that file exists. The program should not alter the contents of the file (if it does exist) in any way.

For simplicity, check only the directory/folder in which your program exists (so do not worry about drives, directory path names and so on).

Hint: A promising way to do this would be to attempt to open the file and observe results."

I've created a file called 'hello.txt' and put it in a seperate folder called 'Test Folder' and tried to get it to work so many times but I'm getting no where. Any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 3
    Post your existing code and what it does, then we'll get somwheres – Awalrod Mar 20 '14 at 00:59
  • Not a clue what do as we've never covered this in class but my code so far. import os filename = raw_input("Please enter the name of the file: ") if os.path.isfile(filename): fh = open(filename, "r") print "File exists!" else: print "File does not exist!" – user3440083 Mar 20 '14 at 01:31
  • Seems you've more than solved the problem in the code you already have. My only question -- if, as stated in the problem, you're looking for the file in the program's folder (and you are) -- why did you create the file in a separate folder? You won't find it there! – Larry Lustig Mar 20 '14 at 02:10

2 Answers2

1
#!/usr/bin/python
import sys
import os.path
fileName = sys.argv[1]
print(os.path.isfile(fileName))

From this question: How do I check whether a file exists using Python?

Community
  • 1
  • 1
Awalrod
  • 268
  • 1
  • 4
  • 14
0

The first thing to do here is decompose the requirements and deal with each separately.

  1. Prompt the user for a filename
  2. Determine whether "that file" exists.
  3. Implicitly - report the answer to 2.

You need to be able to do both (3 should be obvious), and realize that by "that file exists", what is really meant is whether a file with that name exists.

Try either of the two, or both by running the python interpreter. If you still need help, show what you have done.

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29
  • Not a clue what do as we've never covered this in class but my code so far. import os filename = raw_input("Please enter the name of the file: ") if os.path.isfile(filename): fh = open(filename, "r") print "File exists!" else: print "File does not exist!" – user3440083 Mar 20 '14 at 01:31
  • Ok. So you sort of skipped over doing the first two separately. What happened. In the interpreter, you just type filename, after answering the input to see what it got. Then type in the remaining code, properly indented, and hit enter. What did you get? – Fred Mitchell Mar 20 '14 at 01:46
  • 1
    I've managed to eventually sort it. I created my test file using: file = open("newfile.txt", "w") file.write("Hello World!\n") file.close() Then my code was: filename = raw_input("Please enter the name of the file: ") try: file = open(filename, "r") print file.read(12) except IOError: print"File does not exist!" Cheers! – user3440083 Mar 20 '14 at 02:14
  • Yay! The next step on stackoverflow is figuring out how to "show your code". That will be very helpful to you with any future questions. – Fred Mitchell Mar 20 '14 at 03:56