Sunday, December 12, 2010

python - text replace line endings - decode and encode

import os
import sys


#replaces text or line endings (data holds the opened and read file) (try except etc. removed)
f = open(afile, 'r').read()
data = f.decode('iso-8859-1')
data = data.replace('\r\n','\n')
data = data.replace('\r','\n')


#write to file
f = open(afile, 'w')
f.write(data.encode('utf-8'))
f.close()

python - copy file with shutil

import shutil


#copies file1 to file2 without permissions
shutil.copy(file1, file2)



#copies file1 to file2 with permissions
shutil.copy2(file1, file2)

python - delete file

import os
afile = '/filepath/filename'
# get absolute path (does not inlcude symlinks, use realpath instead to eliminate symlinks) of the file
afile = os.path.abspath(afile)
os.remove(afile)



Friday, December 3, 2010

tar compress and uncompress files

uncompress
tar -xvzf afile.tar.gz

compress
tar -czv -f archiv.tgz archiv/

Thursday, December 2, 2010

curl to download a file

curl -C - -O http://server.tld/files/afile.tar.gz

... loads file to the current directory