Python 2.6 on CentOs issues after install - python

I've been teaching myself python recently an came across and example where str.endswith took a tuple as it's first argument, which 2.4 doesn't support. I decided to try and install a newer version of Python on my machine so I was more up to date. The machine is CentOs5.
As my user on the machine (not root) I pulled the package from here: http://www.python.org/ftp/python/2.6.7/, uncompressed it, did ./configure --prefix=/home/myusername/python/compiler/Python-2.6.7-installed, then ran make, make test (all okay) and then finally make altinstall for good measure (I know it shouldn't be necessary to do altinstall as I specified a prefix but really don't want to break regular python on this machine). When it first didn't work I tried the make altinstall as root also, but it made no difference.
When I try to run a script against the binary I just get a bunch of gibberish like this:
./compiler/Python-2.6.7/Lib/test/test_re.pyc : onXtd}|iti|iddddgdS(Nsu" [\u002E\u3002\uFF0E\uFF61]"sa.b.cR$RHRX(R0RÑRÚR RRY(R R7((s#/home/yspendiff/python/compiler/Python-2.6.7/Lib/test/test_re.pyttest_bug_931848as
Cstidd}|i|iid|it|itidd}|i|i id|i|i ddS(Ns\ssa bii(ii(ii(
StopIterationRºRR(R RRÓR tnextRR4t
Rº((s#/home/yspendiff/python/compiler/Python- 2.6.7/Lib/test/test_re.pyttest_bug_581080js
cCsatidd}|i|iid|i|iid|it|idS(Ns.*tasdfii(ii(ii(RRÓR RÝRR4RÞ(R
and perhaps more pertinently lots of lines like these:
./compiler/Python-2.6.7/Lib/test/test_unicode.py : self.assert_(u'asdf' not in '')
./compiler/Python-2.6.7/Lib/test/test_unicode.py : self.assert_('asdf' not in u'')
./compiler/Python-2.6.7/Lib/test/test_unicode.py : self.assert_(u'asdf' not in u'')
./compiler/Python-2.6.7/Lib/test/test_re.py : iter = re.finditer(r".*", "asdf")
./compiler/Python-2.6.7/Lib/test/string_tests.py : self.checkequal(True, 'asdf', '__contains__', 'asdf')
./compiler/Python-2.6.7-installed/lib/python2.6/test/test_unittest.py : loader.loadTestsFromNames(['sdasfasfasdf'])
./compiler/Python-2.6.7-installed/lib/python2.6/test/test_unittest.py : self.assertEqual(str(e), "No module named sdasfasfasdf")
That is just a few random lines out of hundreds. I haven't messed around with any of the default options, have I pulled down a funny version or specified some funny compilation options. How do I turn it off so I can just code in peace!
The code is below if anyone is interested. I just call it with ./Findword.py asdf :
#!/home/myusername/python/compiler/Python-2.6.7-installed/bin/python2.6
### FindWord.py
import os # for curdir() #(A)
import os.path # for join(), isfile() #(B)
import sys # for argv[], exit() #(C)
if len( sys.argv ) != 2: #(D)
print "need a word or a single-quoted phrase to search for" #(E)
sys.exit(1) #(F)
def searchInFile( pattern, dirname, filenames ): #(G)
for name in filenames: #(H)
name = os.path.join( dirname, name ) #(I)
if os.path.isfile( name ) and not name.endswith(('.pdf','.pl')): #(J)
FH = open( name, 'r' ) #(K)
for eachline in FH: #(L)
if ( eachline.find( pattern ) != -1 ): #(M)
print name, ': ', eachline #(N)
os.path.walk( os.curdir, searchInFile, sys.argv[1] ) #(O)

Pretty much exactly what you're asking Python to do is happening. You're telling it to find the word 'asdf' from whatever your current directory is, it's finding it in binary files.

Related

Python 3 long filename as argument (python splits it)

I've been having a problem with taking a command line argument on windows that has a long filename and passing it to a function.
In short the long filename containing spaces is split into separate parts.
I made a fudged together bit of code to give me the command line as a whole, but it's far from ideal as although it works for multiple parameters it doesn't work for LFNs with spaces.
(the cludge was made for a different script, I've just copied it across to this one)
I've been searching for hours on google trying to figure this out as surely someone else has solved this.
I've tried using ArgParse but couldn't get that to give me the filename without splitting it either.
Would someone be good enough to show me some code that demonstrates getting the command line exactly as is (minus the script name) and also getting a full filename.
Thanks,
Adam
[edit..]
I tried putting quotes around it and it failed still. I know from testing the code that it splits the input on the spaces and removes the quotes.
example from a different test:
test.py code:
import sys
print ('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))
for x in range(0, len(sys.argv)):
print("->" + sys.argv[x])
output:
H:\bin>test "test lfn.txt"
Number of arguments: 3 arguments.
Argument List: ['H:\\bin\\test.py', ' test', 'lfn.txt']
->H:\bin\test.py
-> test
->lfn.txt
#
[edit 2]
I think it's a Python on Windows bug as double quoting works, sort of:
H:\bin>test ""test lfn.txt""
Number of arguments: 2 arguments.
Argument List: ['H:\\bin\\test.py', ' "test lfn.txt"']
->H:\bin\test.py
-> "test lfn.txt"
original code posted below.
###############################################################################
# Renames a single file to ISO date format: YYYYMMDD-HHMMSS #
###############################################################################
import datetime, os, sys
def error_filename():
print("filename doesn't exist maybe.")
sys.exit(1)
def error_args():
print("Renames a single file to ISO date format: YYYYMMDD-HHMMSS")
print("Requires 1 parameter, the filename to rename")
sys.exit(2)
def error_rename():
print("Unable to rename")
sys.exit(3)
cmds = ""
for x in range(1, len(sys.argv)):
cmds = cmds + sys.argv[x]
cmds = cmds.strip()
if cmds != "":
d = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
if os.path.isfile(cmds):
fn = cmds.split(os.extsep)
fn[0]=d
newname = d + "." + fn[1]
print(cmds + " -> " + newname)
try:
os.rename(cmds, newname)
except:
error_rename()
else:
error_filename()
else:
error_args()
The error I was having was because Windows 7 was previously defaulting to opening python files in an editor, I changed it manually in the registry to open with python. By doing a clean install on a different machine and letting the python installer set up the path etc it worked fine. The problem lies with a windows registry setting.

os.listdir can't see my directory

I am working on a python script that installs an 802.1x certificate on a Windows 8.1 machine. This script works fine on Windows 8 and Windows XP (haven't tried it on other machines).
I have isolated the issue. It has to do with clearing out the folder
"C:\Windows\system32\config\systemprofile\AppData\LocalLow\Microsoft\CryptURLCache\Content"
The problem is that I am using the module os and the command listdir on this folder to delete each file in it. However, listdir errors, saying the folder does not exist, when it does indeed exist.
The issue seems to be that os.listdir cannot see the LocalLow folder. If I make a two line script:
import os
os.listdir("C:\Windows\System32\config\systemprofile\AppData")
It shows the following result:
['Local', 'Roaming']
As you can see, LocalLow is missing.
I thought it might be a permissions issue, but I am having serious trouble figuring out what a next step might be. I am running the process as an administrator from the command line, and it simply doesn't see the folder.
Thanks in advance!
Edit: changing the string to r"C:\Windows\System32\config\systemprofile\AppData", "C:\Windows\System32\config\systemprofile\AppData", or C:/Windows/System32/config/systemprofile/AppData" all produce identical results
Edit: Another unusual wrinkle in this issue: If I manually create a new directory in that location I am unable to see it through os.listdir either. In addition, I cannot browse to the LocalLow or my New Folder through the "Save As.." command in Notepad++
I'm starting to think this is a bug in Windows 8.1 preview.
I encountered this issue recently.
I found it's caused by Windows file system redirector
and you can check out following python snippet
import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
#Example usage
import os
path = 'C:\\Windows\\System32\\config\\systemprofile\\AppData'
print os.listdir(path)
with disable_file_system_redirection():
print os.listdir(path)
print os.listdir(path)
ref : http://code.activestate.com/recipes/578035-disable-file-system-redirector/
You must have escape sequences in your path. You should use a raw string for file/directory paths:
# By putting the 'r' at the start, I make this string a raw string
# Raw strings do not process escape sequences
r"C:\path\to\file"
or put the slashes the other way:
"C:/path/to/file"
or escape the slashes:
# You probably won't want this method because it makes your paths huge
# I just listed it because it *does* work
"C:\\path\\to\\file"
I'm curious as to how you are able to list the contents with those two lines. You are using escape sequences \W, \S, \c, \s, \A in your code. Try escaping the back slash like this:
import os
os.listdir('C:\\Windows\\System32\\config\\systemprofile\\AppData')

Python: Adding Variable paths into os.system("open"+filename)

I'm trying to make a script where python goes through a directory, finds all files that ends with *ref.SDAT, and then opens them all.
So far, I have the sorting of files process. The wanted files are put into reflist:
import os
import glob
import subprocess
os.chdir("/Users/BabyJ/Desktop/MRSDATA")
reflist = glob.glob('*raw_ref.SDAT')
print "These are the ref files:"
for i in reflist:
os.system('open+%s') %i
I don't know how to structure the syntax so that os.system will open all of the components of the list though.
The % operator wants the 'open+%s' string as its left-hand side. You are offering it the return value of os.system(). Also, I think you wanted a space, not a + in the string.
Try this:
os.system('open %s'%i)
I assuming judging from your use of open that you are on a Mac or Unix system. If that is the case use either of the following to get you up and running.
for i in reflist:
os.system('open ' + '%s' % i)
or:
for i in reflist:
subprocess.call('open ' + '%s' % i, shell = True)
Using subprocess is the better solution as os.system is, though not technically gone from the language, deprecated in Python per the official documentation.
Hope that helps.
EDIT:
If you're using windows sub in start for open.

Learning python and having trouble with 1st program

I wrote this code and it is failing at line 11 on the "target_dir" command with invalid syntax I have a vm ubuntu and I just copy and pasted the code and it worked there but not in my win7 and I am not sure why. I was reading another question with similar code but it had a different error and noticed that someone said that some of these commands where obsolete so I was just wondering if that is so and I would just drop this book and move on to another one i just got.
Thanks in advance for the help,
# Filename: backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['"D:\\Warlock"', 'C:\\Druid'
# Notice we had to use double quotes inside the string for names with spaces in it.
# 2. The backup must be stored in a main backup directory
target_dir = r'C:\Backup' # Remember to change this to what you will be using
# 3. The files are backed up into zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip commnad to put the files in a zip archive
zip_commnad = "7z a -tzip {0} {1}" .format(target, ' '.join(source))
print(zip_command)
#Run the backup
if os.system(zip_command) == 0:
print('Successful backup', target)
else:
print('Backup FAILED')
source = ['"D:\\Warlock"', 'C:\\Druid' is missing an end bracket. Should be source = ['"D:\\Warlock"', 'C:\\Druid'].
Edit: Also,
zip_commnad = "7z a -tzip {0} {1}" .format(target, ' '.join(source))
print(zip_command)
should be
zip_command = "7z a -tzip {0} {1}" .format(target, ' '.join(source))
print(zip_command)
i.e., spell command correctly and fix the indentation. Additionally, although defining the backup paths like you are is not an error, I'd agree with abaumg's comment that using raw strings would be a lot clearer.
In Python, an invalid syntax error often means that there is a syntax error, one or more lines above the line number that is reported.
Use an editor that does parenthesis hightlighting so that you can move your cursor along the line and see where there are missing or too many parentheses, braces, brackets, etc.
Also, you might want to have a look at the os.path module and get rid of the C: and the \ from your filenames. It is possible to make Python code portable between Linux and Windows. The drivenames could come from options (sys.argv) or a config file (ConfigParser) and the \ can be replaced by a single / and then you can use os.path.normcase() to normalize it for the current OS.

running a system command in a python script

I have been going through "A byte of Python" to learn the syntax and methods etc...
I have just started with a simple backup script (straight from the book):
#!/usr/bin/python
# Filename: backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
# 2. The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
Right, it fails. If I run the zip command in the terminal it works fine. I think it fails because the zip_command is never actually run. And I don't know how to run it.
Simply typing out zip_command does not work. (I am using python 3.1)
Are you sure that the Python script is seeing the same environment you have access to when you enter the command manually in the shell? It could be that zip isn't on the path when Python launches the command.
It would help us if you could format your code as code; select the code parts, and click on the "Code Sample" button in the editor toolbar. The icon looks like "101/010" and if you hold the mouse pointer over it, the yellow "tool tip" box says "Code Sample <pre></pre> Ctrl+K"
I just tried it, and if you paste code in to the StackOverflow editor, lines with '#' will be bold. So the bold lines are comments. So far so good.
Your strings seem to contain backslash characters. You will need to double each backslash, like so:
target_dir = 'E:\\Backup'
This is because Python treats the backslash specially. It introduces a "backslash escape", which lets you put a quote inside a quoted string:
single_quote = '\''
You could also use a Python "raw string", which has much simpler rules for a backslash. A raw string is introduced by r" or r' and terminated by " or ' respectively. examples:
# both of these are legal
target_dir = r"E:\Backup"
target_dir = r'E:\Backup'
The next step I recommend is to modify your script to print the command string, and just look at the string and see if it seems correct.
Another thing you can try is to make a batch file that prints out the environment variables, and have Python run that, and see what the environment looks like. Especially PATH.
Here is a suggested example:
set
echo Trying to run zip...
zip
Put those in a batch file called C:\mytest.cmd, and then have your Python code run it:
result_code = os.system("C:\\mytest.cmd")
print('Result of running mytest was code', result_code)
If it works, you will see the environment variables printed out, then it will echo "Trying to run zip...", then if zip runs it will print a message with the version number of zip and how to run it.
zip command only work in linux not for windows.. thats why it make an error..

Categories