>>> import os
>>> pth="/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te.tuerces"
>>> os.path.split(os.path.dirname(pth))[1]
'alma'
>>>
Mostly Linux & Python syntax notes and hyperlinks.
Thursday, June 23, 2011
python: get the last directory name in a path
If I want to pull just the parent directory name out of a long path to a file:
Friday, June 17, 2011
python: create a dummy file in one line
open('onestep.txt','w').close()from a comment on http://rogermcd.wordpress.com/2008/07/20/creating-a-file-with-python/
Friday, May 27, 2011
Linux: grep for $
This was harder than grep for tab.
I found instructions to escape the special character, but none of these
worked until I read "Note that a $ sign loses its meaning if characters follow it" and remembered I was looking for a $ followed by a capital letter. So this works:
but I still don't know how to find a $ by itself.
I found instructions to escape the special character, but none of these
grep $ *
grep \$ *
grep "$" *
grep "\$" *
grep - $ *
grep "$" *
grep '$' *
egrep '$' *
worked until I read "Note that a $ sign loses its meaning if characters follow it" and remembered I was looking for a $ followed by a capital letter. So this works:
grep '$[A-Z]' *but I still don't know how to find a $ by itself.
Wednesday, May 25, 2011
Python: os.path.lexists() vs os.path.exists() before os.symlink()
We were having intermittent failures with shutil.copy(src,dest) and decided to replace it with just a symbolic link, using os.symlink(src,dest). This triggered an error when we tried to create a symbolic link with the name of an existing file. So the code checks first. It uses os.path.lexists(), since that will return True for broken as well as valid links. os.path.exists() returns True for valid links, False for broken ones.
The os.remove() works whether or not the link is broken or valid or an actual file.
The os.remove() works whether or not the link is broken or valid or an actual file.
if os.path.lexists(dest):
os.remove(dest)
os.symlink(src,dest)
(For more detail, see also June2013)Friday, April 15, 2011
Rohit Bhardwaj's talk on Android programming last night at NE-JUG
Listing other dl.dropbox URL's for a zip of samples source code.
His twitter:
http://twitter.com/#!/rbhardwaj1
My notes:
My notes:
- Each App has its own 'Linux User' so it can't poach security info.
- All the data locked in for that user.
- There are ways to share data between apps.
- APK
- instead of EXE
- can be stored in SD-card
- testing: need to test with at least 50 devices
- use ADBridge
- autogenerated R.java has all References.
- OnCreate(Bundle savedInstance)
- Honeycomb/Android 3 like iPad, multitasking
- Android marketplace $25 to sell your apps.
Tuesday, March 1, 2011
Python: appending column to end of tab-separated csv
Given this input file:
$ cat list.csv a b c d e f g h iUsing this code:
import os,csv
def test_fcn() :
try:
tabReader = csv.reader( open("list.csv", "rb"), delimiter='\t', quoting=csv.QUOTE_NONE )
fd = open("out.csv", "wb+")
tabWriter = csv.writer(fd, delimiter='\t', quoting=csv.QUOTE_NONE)
if tabReader != None :
for record in tabReader:
record.append("new_thing")
tabWriter.writerow(record)
fd.close()
return
except Exception,e :
print "Exception : " + str(e)
def main():
test_fcn()
if __name__ == '__main__':
import sys
main(*sys.argv[1:])
Here's the output:$ cat out.csv a b c new_thing d e f new_thing g h i new_thing
Friday, January 28, 2011
Linux: Using whereis to find ifconfig
If something isn't in default path you still may be able to find it with whereis:
$ ifconfig
bash: ifconfig: command not found
$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
$ /sbin/ifconfig
eth2 Link encap:Ethernet HWaddr ...
Tuesday, January 4, 2011
Linux: to grep for dash, use grep --
For example, to search for python code containing the string "-D":
grep -- -D *.pyFrom http://stackoverflow.com/questions/2427913/grepping-for-string-containing-dash
Wednesday, December 1, 2010
Subscribe to:
Posts (Atom)