Files and directories

This module contains functions for working with files and directories. Check the standard modules os.path for more path functions.

Examples

Writing files

Write string to a file

import sy.path

# Write 'hello world' to /tmp/hello
sy.path.dump('/tmp/hello', 'hello world\n')

# Appending bye world' to the end of /tmp/hello
sy.path.append('/tmp/hello', 'bye world\n')

Extract an archive

import sy.path

# Extract /var/tmp/huge.tar.bz2 to /var/tmp
sy.path.extract('/var/tmp/huge.tar.bz2', '/var/tmp')

Reading files

Getting file content

import sy.path

# Return content of file as a string
sy.path.slurp('/tmp/hello')
'hello world\nbye world\n'

# Return lines from file
sy.path.lines('/tmp/hello')
['hello world\n', 'bye world\n']

# Return lines without newline character
sy.path.lines('/tmp/hello', newline=False)
['hello world', 'bye world']

Checking content

import sy.path

# Check if file contains pattern
sy.path.contains('/tmp/hello', 'hell.*world')
True

# Get the md5 sum of a file
sy.path.md5sum('/etc/passwd')
'dad86c61eea237932f201009e5431609'

# Check if the content of two files are different
sy.path.md5sum('/etc/passwd') == sy.path.md5sum('/etc/passwd.old')
False

Replace content in text files

These functions change the content on files in-place. They try to be as safe as possible and should fail nicely without corrupting files.

import sy.path

# Remove all lines matching 'b.*world' from file
sy.path.remove_lines('/tmp/hello', r'b.*world')
1

# Replace whole lines matching 'ello world' with 'alo world'
sy.path.replace_lines('/tmp/hello', r'.*ello world', r'alo world')
1

# Switch places on 'alo' and 'world'
sy.path.replace('/tmp/hello', r'(\w+) (\w+)', r'\2 \1')
1

Path operations

import sy.path

# Clean up and expand paths
sy.path.expandpath('$JAVA_HOME/bin')
'/opt/java6/bin'
sy.path.expandpath('~/.profile')
'/export/home/john/.profile'
sy.path.expandpath('/var/tmp/../adm')
'/var/adm'

# Make 'john' own '/tmp/hello'
sy.path.chown('/tmp/hello', 'john')

# Change group on '/tmp/hello' to 100
sy.path.chown('/tmp/hello', None, 100)

# Get the owner of '/tmp/hello'
sy.path.owner('/tmp/hello')
'john'

# Get the group GID of '/tmp/hello'
sy.path.group('/tmp/hello', gid=True)
100

# Return the permissions on '/tmp/hello'
sy.path.mode('/tmp/hello')
0644

# Remove files, rm -rf style
sy.path.rmtree('/tmp/junk')

sy.path content

synopsis:Simplify operations on files and directories
sy.path.dump(path, content, binary=False, encoding=None, newline=os.linesep)

Dump content to a file.

Writes content to file, overwriting if the file exists. Use append() to add to the of the file.

Parameters:
  • binary – Write binary data to the file
  • encoding – If content is unicode, specifies how it should be encoded in the file. Raises an assertion error if specified for non-unicode content
  • newline – Convert all newlines types (Windows or Mac) to this. Default \n on Unix. If set to None, the content is written without changing any newlines.
sy.path.append(*args, **kwargs)

Append content to file. Same arguments as to dump()

sy.path.chown(path, owner, group)

Change owner and group of a file or directory.

If owner or group is None it is left unchanged. If both are set to None this function does nothing.

If you use username or groupname that user or group must exist on the current system.

Parameters:
  • owner – Uid or username of the new owner.
  • group – Gid or groupname of the new owner.
sy.path.contains(path, pattern, encoding=None)

Returns True if the file contains a line that matches the string or compiled pattern.

Parameters:
  • path – Path to the file
  • pattern – String or compiled pattern
sy.path.dump(path, content, append=False, binary=False, encoding=None, newline='\n')

Dump content to a file.

Writes content to file, overwriting if the file exists. Use append() to add to the of the file.

Parameters:
  • binary – Write binary data to the file
  • encoding – If content is unicode, specifies how it should be encoded in the file. Raises an assertion error if specified for non-unicode content
  • newline – Convert all newlines types (Windows or Mac) to this. Default \n on Unix. If set to None, the content is written without changing any newlines.
sy.path.expandpath(path)

Clean up path. Expands environment vars like $JAVA_HOME, does tilde expansion for paths like “~/.bashrc” and normalizes the path

sy.path.extract(archive, dir)

Unpack a tar or zip file to the specified directory.

Tarfiles compressed with gzip, bzip2 or compress are uncompressed.

Parameters:
  • archive – Path to the archive
  • dir – Directory where the archive will be uncompressed
sy.path.group(path, gid=False)

Return the name of the group that owns the file or directory.

Parameters:
  • gid – If True return the gid as an integer
sy.path.lines(path, encoding=None, newline=True)

Return list of lines from file.

Parameters:
  • newline – If true, keep newline characters for every line. Default is True. All newline characters (\r, \r\n) are replaced by \n.

Same arguments as to slurp()

sy.path.md5sum(path, hex=True)

Calculate md5 sum for a file.

Parameters:
  • hex – Return the digest in hexadecimal suitable for writing to text files. Default True.
sy.path.mode(path)

Permission mode of the file or directory.

Returns:octal representation of the mode, ie 0644
sy.path.owner(path, uid=False)

Return username that owns the file or directory.

Parameters:
  • uid – If True return the uid as an integer
sy.path.remove_lines(path, matching, encoding=None)

Remove lines from the path that matches matching

Note

The line will match if the pattern is anywhere on the line. See re.search() in the standard library.

Parameters:
  • path – Path to the file
  • matching – String to search for (or compiled regular expression)
Returns:

Number of lines removed

sy.path.replace(path, match, replacement, encoding=None)

Replace all occurances of match with replacement. Using re.sub(), backreferences in the replacement will work as normal:

# change login of all users to 'leif'
rx = re.compile(r'^\w+:(.*)', re.MULTILINE)
replace('/etc/passwd', rx, r'leif:\1') 

Note

Make sure your replacement uses a raw string (r'') if it contains backreferences.

Parameters:
  • path – Path to the file
  • match – String to match
  • replacement – String to replace match with
Returns:

Number of replacements made

sy.path.replace_lines(path, matching, replacement, encoding=None)

Replace lines that contain the string matching with replacement

Note

The line will match if the pattern is anywhere on the line. See re.search() in the standard library.

Parameters:
  • path – Path to the file
  • matching – String or compiled regular expression.
  • replacement – Line to replace with, newline character will automatically be added if missing.
Returns:

Number of lines replaced

sy.path.rmtree(path, force=False)

Removes a directory and everything below it.

Parameters:
  • force – Ignores all errors, like rm -rf
sy.path.slurp(path, encoding=None, binary=False)

Return a file as a string

Parameters:
  • path – Path to file
  • encoding – The encoding of the file. Default is None which returns the file as a 8-bit string which works well for latin1 and asci.
  • binary – Treat the file as a binary, no conversions will be made