This module contains functions for working with files and directories. Check the standard modules os.path for more path functions.
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')
import sy.path
# Extract /var/tmp/huge.tar.bz2 to /var/tmp
sy.path.extract('/var/tmp/huge.tar.bz2', '/var/tmp')
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']
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
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
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')
synopsis: | Simplify operations on files and directories |
---|
Dump content to a file.
Writes content to file, overwriting if the file exists. Use append() to add to the of the file.
Parameters: |
|
---|
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: |
|
---|
Returns True if the file contains a line that matches the string or compiled pattern.
Parameters: |
|
---|
Dump content to a file.
Writes content to file, overwriting if the file exists. Use append() to add to the of the file.
Parameters: |
|
---|
Clean up path. Expands environment vars like $JAVA_HOME, does tilde expansion for paths like “~/.bashrc” and normalizes the path
Unpack a tar or zip file to the specified directory.
Tarfiles compressed with gzip, bzip2 or compress are uncompressed.
Parameters: |
|
---|
Return the name of the group that owns the file or directory.
Parameters: |
|
---|
Return list of lines from file.
Parameters: |
|
---|
Same arguments as to slurp()
Calculate md5 sum for a file.
Parameters: |
|
---|
Permission mode of the file or directory.
Returns: | octal representation of the mode, ie 0644 |
---|
Return username that owns the file or directory.
Parameters: |
|
---|
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: |
|
---|---|
Returns: | Number of lines removed |
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: |
|
---|---|
Returns: | Number of replacements made |
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: |
|
---|---|
Returns: | Number of lines replaced |
Removes a directory and everything below it.
Parameters: |
|
---|
Return a file as a string
Parameters: |
|
---|