File Operations in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 4: Line 4:


=Check whether a File Exists=
=Check whether a File Exists=
==<tt>os.path</tt>==
<syntaxhighlight lang='python'>
import os.path
file_exists = os.path.exists(path_to_file)
</syntaxhighlight>
Returns <code>True</code> or <code>False</code>.
==<tt>pathlib</tt>==
<code>Path</code> represents a filesystem path that offers methods to do system calls on path objects. Depending on your system, instantiating a <code>Path</code> will return either a <code>PosixPath</code> or a <code>WindowsPath</code> object. You can also instantiate a <code>PosixPath</code> or <code>WindowsPath</code> directly, but cannot instantiate a <code>WindowsPath</code> on a POSIX system or vice versa.
<syntaxhighlight lang='python'>
from pathlib import Path
path = Path(path_to_file)
path.is_file()
</syntaxhighlight>
New <code>Path</code> instance can be constructed from a <code>Path</code> instance:
<syntaxhighlight lang='python'>
path = Path('.')
path2 = Path(path, './some-file.txt')
</syntaxhighlight>
Convert the <code>Path</code> to a string with <code>str()</code>:
<syntaxhighlight lang='python'>
path = Path('.')
print(str(path))
</syntaxhighlight>
====<tt>resolve(strict=False)</tt>====
Relative paths ("../../..") can be "resolved" with:
<syntaxhighlight lang='python'>
path = Path('/Users/ovidiu/..')
print(path.resolve()) # will display "/Users"
</syntaxhighlight>
====<tt>mkdir(mode=0o777, parents=False, exist_ok=False)</tt>====
Create a directory, including its non-existent parents if required.
<syntaxhighlight lang='py'>
d = Path('somedir')
d.mkdir(0o700, True, False)
</syntaxhighlight>
Setting <code>parents</code> to <code>True</code> will create intermediate missing directories if necessary. By default, <code>parents</code> is <code>False</code>.
The method fails if the directory already exists, unless <code>exist_ok</code> is set to <code>True</code>.
===Other <tt>pathlib</tt> Methods===
* <code>cwd()</code>
* <code>home()</code>
* <code>samefile(other_path)</code>
* <code>iterdir()</code>
* <code>glob(pattern)</code>
* <code>rglob(pattern)</code>
* <code>absolute()</code>
* <code>stat()</code>
* <code>group()</code>
* <code>open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)</code>
* <code>read_bytes()</code>
* <code>read_text(encoding=None, errors=None)</code>
* <code>write_bytes(data)</code>
* <code>write_text(data, encoding=None, errors=None)</code>
* <code>touch(mode=0o666, exist_ok=True)</code>
* <code>chmod(mode)</code>
* <code>lchmod(mode)</code>
* <code>unlink(missing_ok=False)</code>
* <code>rmdir()</code>
* <code>lstat()</code>
* <code>link_to(target)</code>
* <code>rename(target)</code>
* <code>replace(target)</code>
* <code>symlink_to(target, target_is_directory=False)</code>
* <code>exists()</code>
* <code>is_dir()</code>
* <code>is_file()</code>
* <code>is_mount()</code>
* <code>is_symlink()</code>
* <code>is_block_device()</code>
* <code>is_char_device()</code>
* <code>is_fifo()</code>
* <code>is_socket()</code>
* <code>expanduser()</code>


=Reading/Writing from/to Files=
=Reading/Writing from/to Files=

Revision as of 22:01, 2 March 2022

Internal

Check whether a File Exists

Reading/Writing from/to Files

Read

Understand this idiom. What does with do? Does it automatically close the file when it exits the block? Apparently this is a "context manager".

with open('somefile.txt', 'rt') as f:
  text = f.read()
  print(text)

 f = open(''filename'', ''mode'')
 c = f.read()
 f.close()
f = open('somefile', 'rt')
c = f.read()
f.close()

Mode: "r", "w", "x", etc. "t" text, "b" binary

Write

f = open('/Users/ovidiu/tmp/out.json', 'wt')
f.write("test\n")
f.close()

Working Directory

import os
print('getcwd:', os.getcwd())

Also see:

os

The Path of the Running Script File

print('__file__:', __file__)

Paths

os.path.basename returns the file name from the file path:

import os
print(os.path.basename(__file__))

os.path.dirname returns the directory name from the file path.

import os
print(os.path.dirname(__file__))

os.path.abspath return the absolute path from a file path.

os.path.splittext returns the file name from the file path.

Use the pathlib module to extract directory name.

Removing Files

import os
os.remove("somefile.txt")
os.rmdir("somedir") # removes an empty directory
shutil.rmtree() # deletes a directory and all its contents.

Path objects from the Python 3.4+ pathlib module also expose these instance methods:

pathlib.Path.unlink()  # removes a file or symbolic link.
pathlib.Path.rmdir() # removes an empty directory.

Recursively Copy a Directory

Recursively Copy a Directory with shutil

Temporary Files

https://docs.python.org/3/library/tempfile.html

Create a named temporary file, write in it and return the name. The file is not automatically deleted after close(), the caller will need to delete it:

import tempfile
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as t:
  t.write(something)
  return t.name