Have you noticed that you don’t get the error if you run instead of This is because sys.argv will read ./script.py in the former case, which gives os.path.dirname something to work with. When you don’t specify a path, sys.argv reads simply script.py, and os.path.dirname cannot determine a path.
I’m writing a simple script that is trying to extract the first element from the second column of a .txt input file. My input file looks like this: I want my delimiter to be a comma. When I print the length of the line out, I’m getting 5 elements (as expected). However, whenever I try … Read more
When I try to remove entries from nonexistent file, I met the follow error: Cannot remove entries from nonexistent file /usr/local/bin/anaconda2/lib/python2.7/site-packages/easy-install.pth How could I solve it? Thanks in advance.
r means the string will be treated as raw string. See the official Python 2 Reference about “String literals”: When an ‘r’ or ‘R’ prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r”\n” consists of two characters: a backslash and a lowercase ‘n’. String quotes can be … Read more
I had the same problem. When I upgraded python3 through Homebrew, I started getting this: I had the same conflict with Python somehow being installed in /Library/Framework/Python.framework. I just did a brew link overwrite and everything is working fine now. There is some info about what to do with the Python version in the /Library/Framework/Python.framework here. I guess you could … Read more
The problem is that you are using next and readline on the same file. As the docs say: . As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right. The fix is trivial: replace next with readline.
My python was corrupted due to some module. So I planned to re-installed or remove the python completely from my Ubuntu 16.04 machine. But sudo apt-get install –reinstall python2.7 command was also failing and was throwing same error. So I finally Did few hacks and cracks. Here are the steps -Removing all python version manually Updating Ubuntu … Read more
tl;dr: Fix this issue by doing one of the following: type hash -r python, OR log out and log in. EDIT: An answer to my related question makes it clear what’s happening here. When you install a new version of python, you may need to run hash -r python to tell bash to reset the “cached” location to the python executable. In my … Read more
TL;DR input function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input function in Python 2.7, which will not evaluate the read strings. If you are using Python 3.x, raw_input has been renamed to input. Quoting the Python 3.0 release notes, raw_input() was renamed to input(). That is, the new input() function reads a … Read more
The problem isn’t your use of not, it’s that or doesn’t mean what you think it does (and if you think it through, it couldn’t): You’re asking whether the expression (“StatusRequest” or “StatusResponse”) appears in line. But that expression is just the same thing as “StatusRequest”. Put it in English: you’re not trying to say “if neither of these is in line”. Python … Read more