-3

For whatever reason almost nothing is happening when I run this program. It seems incredibly straight forward and simple and It's driving me insane that It is not working.

fin = open("input.txt", 'r')


print("asd;lkfja;sdlkfj")

read = True

for line in fin.readlines():
    print(line)
    begComment = line.find("/*")
    print(begComment)
    endComment = line.find("*/")
    if(begComment != -1):
        print("found one")
        if(endComment != -1):
            line = line[:begComment] + line[endComment:]



    print(line)

fin.close()

This seems like a really stupid question. I just don't understand why It isnt working.

This is the contents of my input.txt file, its in the same directory as the python file

asdfjas;dlkfja;sdlkfj /*;alksdjf;alksdjf*/
 \n
 a;sldkfj;akdsjf
 asd;lfkja;sdlkjf
 asd;flkja;dklfja;lksdjf;alkjsdf;lkajdsf;lkaj
 as;dlfkj;asdlkf;akj

all that is output by this is the initial print statement (asd;lkfja;sdlkfj)

mhm.sherpa
  • 89
  • 1
  • 9
  • Try providing a full path to the file on the first line... – Nir Alfasi May 31 '16 at 18:26
  • where you're running your .py when you do a dir can you see input.txt? or linux ls can you see input.txt? – FirebladeDan May 31 '16 at 18:29
  • 1
    Your code "works" fine. We need [MCVE]. Is your indentation consistent (no tabs and spaces mixed, indentation level as in above code snippet? – Łukasz Rogalski May 31 '16 at 18:29
  • I can't reproduce your problem on Windows with Python 3.4 – illright May 31 '16 at 18:29
  • Btw. Absence of file would raise `EnvironmentError` so it's not that (or OP does not tell whole story). – Łukasz Rogalski May 31 '16 at 18:31
  • running python 3.5.1, downloaded it about 30 minutes ago on my work computer. This is just a mind-numbly non issue that is making my day hell – mhm.sherpa May 31 '16 at 18:33
  • Maybe try running the script with root access (if on linux)? I can't re-create the problem in Python 3.5.1. Does your script output *anything*? Any errors? What are you doing to execute the script? Maybe the script is being executed incorrectly (`nohup`, or something similar)? – Ben Schwabe May 31 '16 at 18:34
  • What is a result of `fin.readlines()`? You may call it outside of loop and simply `print()`. Isn't it empty list? – Łukasz Rogalski May 31 '16 at 18:34
  • It does output the first print command. Thats what gets me as its obviously running correctly. Im going to reinstall python and see if that helps @Rogalski I think we are getting somewhere, when I print the readlines statement it outputs an empty array, Im keeping the input.txt in the same directory as the .py file, is this incorrect? – mhm.sherpa May 31 '16 at 18:35
  • @NotYourDuck you were correct in putting it in the same directory as the .py file. It seems like you aren't able to open the file. give the file administrative (root) access. Maybe it doesn't have permission to open the file? – Ben Schwabe May 31 '16 at 18:38
  • It'll open file from current working dir, not current location of `*.py` file. Often they are synonymous, sometimes they're not. Try `import os` and check value of `os.getcwd()`. After that check if, by pure accident, empty `input.txt` is not in that directory. – Łukasz Rogalski May 31 '16 at 18:38
  • Shouldn't lack of permissions raise `IOError: Permission Denied`? I find it hard to believe Python would cover up errors silently. – Łukasz Rogalski May 31 '16 at 18:40
  • It looks like it is trying to run in my notepad++ directory, weird. It did raise a permission denied error at first but I fixed that by running notepad++ as admin – mhm.sherpa May 31 '16 at 18:40
  • @NotYourDuck now we are getting somewhere! give the full (absolute) path in your open() statement. See if that fixes it. – Ben Schwabe May 31 '16 at 18:41
  • the command I am using to run the script from notepad++'s "run" function is "cmd /K python "$(FULL_CURRENT_PATH)" – mhm.sherpa May 31 '16 at 18:46
  • 1
    http://stackoverflow.com/questions/4103085/getting-nppexec-to-understand-path-of-the-current-file-in-notepad-for-python Here's your duplicate, you're welcome. I cannot close as duplicate since I already closed it as "cannot reproduce", and after retraction I won't be able vote again. – Łukasz Rogalski May 31 '16 at 18:52
  • I suspect it has something to do with an unexpected working directory, but python 3.5.1 throws a `FileNotFoundError` when it can't open a file, which you don't appear to be getting (unless notepad++ is somehow suppressing error outputs) – Ben Schwabe May 31 '16 at 18:58

2 Answers2

0

Try placing the full (absolute) path to your file in the open() statement:

fin = open("/full/working/path/to/the/file/input.txt","r") #(try C:/full/working/[...] for windows builds)

#continue code...

When using open(), python assumes the current working directory. Depending on what IDE your are using and how you are executing the file, this can produce unexpected results. If you're using notepad++ (which from the comments, it looks like you are), python might be opening the file in the program directory of notepad++, rather than in the directory that your .py file is located. Absolute paths fix this by forcing python to look in a very specific directory. I don't have notepad++ installed, but I have encountered this problem before on other low-powered IDEs.

Ben Schwabe
  • 1,283
  • 1
  • 21
  • 36
-3

You're calling readlines at every iteration, break that out of the for loop.

lines = file.readlines()
for line in lines:
CamJohnson26
  • 1,119
  • 1
  • 15
  • 40
  • Nope, OP is doing it correctly. That's not how for loops work in Python. – Morgan Thrapp May 31 '16 at 18:29
  • 2
    the line `for line in file.readlines()` operates by resolving `file.readlines()` first, then resolving the resulting `for line in [list,returned,by,file.readlines()]`, so your suggestion does the same thing. The list just is not persistent after the `for` loop ends in the original example. – Ben Schwabe May 31 '16 at 18:37