In [40]:
%autosave 10
%matplotlib inline
import matplotlib.pyplot as plt
Autosaving every 10 seconds
In [1]:
import os
In [2]:
dir(os)
Out[2]:
['CLD_CONTINUED',
 'CLD_DUMPED',
 'CLD_EXITED',
 'CLD_TRAPPED',
 'DirEntry',
 'EX_CANTCREAT',
 'EX_CONFIG',
 'EX_DATAERR',
 'EX_IOERR',
 'EX_NOHOST',
 'EX_NOINPUT',
 'EX_NOPERM',
 'EX_NOUSER',
 'EX_OK',
 'EX_OSERR',
 'EX_OSFILE',
 'EX_PROTOCOL',
 'EX_SOFTWARE',
 'EX_TEMPFAIL',
 'EX_UNAVAILABLE',
 'EX_USAGE',
 'F_LOCK',
 'F_OK',
 'F_TEST',
 'F_TLOCK',
 'F_ULOCK',
 'MutableMapping',
 'NGROUPS_MAX',
 'O_ACCMODE',
 'O_APPEND',
 'O_ASYNC',
 'O_CLOEXEC',
 'O_CREAT',
 'O_DIRECT',
 'O_DIRECTORY',
 'O_DSYNC',
 'O_EXCL',
 'O_LARGEFILE',
 'O_NDELAY',
 'O_NOATIME',
 'O_NOCTTY',
 'O_NOFOLLOW',
 'O_NONBLOCK',
 'O_RDONLY',
 'O_RDWR',
 'O_RSYNC',
 'O_SYNC',
 'O_TRUNC',
 'O_WRONLY',
 'POSIX_FADV_DONTNEED',
 'POSIX_FADV_NOREUSE',
 'POSIX_FADV_NORMAL',
 'POSIX_FADV_RANDOM',
 'POSIX_FADV_SEQUENTIAL',
 'POSIX_FADV_WILLNEED',
 'PRIO_PGRP',
 'PRIO_PROCESS',
 'PRIO_USER',
 'P_ALL',
 'P_NOWAIT',
 'P_NOWAITO',
 'P_PGID',
 'P_PID',
 'P_WAIT',
 'PathLike',
 'RTLD_DEEPBIND',
 'RTLD_GLOBAL',
 'RTLD_LAZY',
 'RTLD_LOCAL',
 'RTLD_NODELETE',
 'RTLD_NOLOAD',
 'RTLD_NOW',
 'R_OK',
 'SCHED_BATCH',
 'SCHED_FIFO',
 'SCHED_IDLE',
 'SCHED_OTHER',
 'SCHED_RESET_ON_FORK',
 'SCHED_RR',
 'SEEK_CUR',
 'SEEK_END',
 'SEEK_SET',
 'ST_APPEND',
 'ST_MANDLOCK',
 'ST_NOATIME',
 'ST_NODEV',
 'ST_NODIRATIME',
 'ST_NOEXEC',
 'ST_NOSUID',
 'ST_RDONLY',
 'ST_RELATIME',
 'ST_SYNCHRONOUS',
 'ST_WRITE',
 'TMP_MAX',
 'WCONTINUED',
 'WCOREDUMP',
 'WEXITED',
 'WEXITSTATUS',
 'WIFCONTINUED',
 'WIFEXITED',
 'WIFSIGNALED',
 'WIFSTOPPED',
 'WNOHANG',
 'WNOWAIT',
 'WSTOPPED',
 'WSTOPSIG',
 'WTERMSIG',
 'WUNTRACED',
 'W_OK',
 'XATTR_CREATE',
 'XATTR_REPLACE',
 'XATTR_SIZE_MAX',
 'X_OK',
 '_Environ',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_execvpe',
 '_exists',
 '_exit',
 '_fspath',
 '_fwalk',
 '_get_exports_list',
 '_putenv',
 '_spawnvef',
 '_unsetenv',
 '_wrap_close',
 'abc',
 'abort',
 'access',
 'altsep',
 'chdir',
 'chmod',
 'chown',
 'chroot',
 'close',
 'closerange',
 'confstr',
 'confstr_names',
 'cpu_count',
 'ctermid',
 'curdir',
 'defpath',
 'device_encoding',
 'devnull',
 'dup',
 'dup2',
 'environ',
 'environb',
 'error',
 'execl',
 'execle',
 'execlp',
 'execlpe',
 'execv',
 'execve',
 'execvp',
 'execvpe',
 'extsep',
 'fchdir',
 'fchmod',
 'fchown',
 'fdatasync',
 'fdopen',
 'fork',
 'forkpty',
 'fpathconf',
 'fsdecode',
 'fsencode',
 'fspath',
 'fstat',
 'fstatvfs',
 'fsync',
 'ftruncate',
 'fwalk',
 'get_blocking',
 'get_exec_path',
 'get_inheritable',
 'get_terminal_size',
 'getcwd',
 'getcwdb',
 'getegid',
 'getenv',
 'getenvb',
 'geteuid',
 'getgid',
 'getgrouplist',
 'getgroups',
 'getloadavg',
 'getlogin',
 'getpgid',
 'getpgrp',
 'getpid',
 'getppid',
 'getpriority',
 'getresgid',
 'getresuid',
 'getsid',
 'getuid',
 'getxattr',
 'initgroups',
 'isatty',
 'kill',
 'killpg',
 'lchown',
 'linesep',
 'link',
 'listdir',
 'listxattr',
 'lockf',
 'lseek',
 'lstat',
 'major',
 'makedev',
 'makedirs',
 'minor',
 'mkdir',
 'mkfifo',
 'mknod',
 'name',
 'nice',
 'open',
 'openpty',
 'pardir',
 'path',
 'pathconf',
 'pathconf_names',
 'pathsep',
 'pipe',
 'pipe2',
 'popen',
 'posix_fadvise',
 'posix_fallocate',
 'pread',
 'preadv',
 'putenv',
 'pwrite',
 'pwritev',
 'read',
 'readlink',
 'readv',
 'register_at_fork',
 'remove',
 'removedirs',
 'removexattr',
 'rename',
 'renames',
 'replace',
 'rmdir',
 'scandir',
 'sched_get_priority_max',
 'sched_get_priority_min',
 'sched_getaffinity',
 'sched_getparam',
 'sched_getscheduler',
 'sched_param',
 'sched_rr_get_interval',
 'sched_setaffinity',
 'sched_setparam',
 'sched_setscheduler',
 'sched_yield',
 'sendfile',
 'sep',
 'set_blocking',
 'set_inheritable',
 'setegid',
 'seteuid',
 'setgid',
 'setgroups',
 'setpgid',
 'setpgrp',
 'setpriority',
 'setregid',
 'setresgid',
 'setresuid',
 'setreuid',
 'setsid',
 'setuid',
 'setxattr',
 'spawnl',
 'spawnle',
 'spawnlp',
 'spawnlpe',
 'spawnv',
 'spawnve',
 'spawnvp',
 'spawnvpe',
 'st',
 'stat',
 'stat_result',
 'statvfs',
 'statvfs_result',
 'strerror',
 'supports_bytes_environ',
 'supports_dir_fd',
 'supports_effective_ids',
 'supports_fd',
 'supports_follow_symlinks',
 'symlink',
 'sync',
 'sys',
 'sysconf',
 'sysconf_names',
 'system',
 'tcgetpgrp',
 'tcsetpgrp',
 'terminal_size',
 'times',
 'times_result',
 'truncate',
 'ttyname',
 'umask',
 'uname',
 'uname_result',
 'unlink',
 'unsetenv',
 'urandom',
 'utime',
 'wait',
 'wait3',
 'wait4',
 'waitid',
 'waitid_result',
 'waitpid',
 'walk',
 'write',
 'writev']
In [3]:
os.listdir()
Out[3]:
['.ipynb_checkpoints',
 '20.10.19',
 'freddi.dat',
 'diagram.csv',
 'cat.jpg',
 'V404Cyg.txt.gz',
 'Untitled.html',
 'io.ipynb',
 'io.html']
In [4]:
os.listdir('20.10.19')
Out[4]:
['mymodule.py',
 'myscript.py',
 'numpy-submodules.html',
 'numpy-submodules.ipynb',
 'other_script.py']
In [7]:
for root, dirs, files in os.walk('.'):
    print(root, dirs, files)
. ['.ipynb_checkpoints', '20.10.19'] ['freddi.dat', 'diagram.csv', 'cat.jpg', 'V404Cyg.txt.gz', 'Untitled.html', 'io.ipynb', 'io.html']
./.ipynb_checkpoints [] ['numpy-submodules-checkpoint.ipynb', 'io-checkpoint.ipynb']
./20.10.19 [] ['mymodule.py', 'myscript.py', 'numpy-submodules.html', 'numpy-submodules.ipynb', 'other_script.py']
In [8]:
os.path
Out[8]:
<module 'posixpath' from '/opt/conda/lib/python3.7/posixpath.py'>
In [9]:
import pathlib
In [13]:
d = '20.10.19'
fname = 'other_script.py'
os.path.join(d, fname)
Out[13]:
'20.10.19/other_script.py'
In [14]:
# ПЛОХО
d + '/' + fname
Out[14]:
'20.10.19/other_script.py'
In [15]:
dir(os.path)
Out[15]:
['__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_get_sep',
 '_joinrealpath',
 '_varprog',
 '_varprogb',
 'abspath',
 'altsep',
 'basename',
 'commonpath',
 'commonprefix',
 'curdir',
 'defpath',
 'devnull',
 'dirname',
 'exists',
 'expanduser',
 'expandvars',
 'extsep',
 'genericpath',
 'getatime',
 'getctime',
 'getmtime',
 'getsize',
 'isabs',
 'isdir',
 'isfile',
 'islink',
 'ismount',
 'join',
 'lexists',
 'normcase',
 'normpath',
 'os',
 'pardir',
 'pathsep',
 'realpath',
 'relpath',
 'samefile',
 'sameopenfile',
 'samestat',
 'sep',
 'split',
 'splitdrive',
 'splitext',
 'stat',
 'supports_unicode_filenames',
 'sys']
In [16]:
os.path.exists(os.path.join(d, fname))
Out[16]:
True
In [20]:
?os.removedirs
In [17]:
import shutil
In [21]:
?shutil.rmtree
In [22]:
shutil.copy
Out[22]:
<function shutil.copy(src, dst, *, follow_symlinks=True)>
In [23]:
shutil.move
Out[23]:
<function shutil.move(src, dst, copy_function=<function copy2 at 0x7faacf6c6680>)>
In [24]:
!ls
20.10.19  diagram.csv  io.html	 Untitled.html
cat.jpg   freddi.dat   io.ipynb  V404Cyg.txt.gz
In [25]:
fd = open('diagram.csv')
text = fd.read()
fd.close()
In [27]:
print(text[:100])
0.036950897,-19.812012
0.03500012,32.59825
0.2093362,42.75399
0.25276598,9.813249
0.2743527,-36.1325
In [28]:
# НЕПИТОНОВО

def read_file(file_path):
    try:
        fd = open(file_path)
        text = fd.read()
        x = text[100]
        raise RuntimeError('ERROR')
    # except RuntimeError as e:
    #     print(e)
    #     raise ValueError('Value Error') from e
    finally:
        fd.close()
    return text
In [31]:
with open('diagram.csv') as fd:
    text = fd.read()
fd.closed
Out[31]:
True
In [32]:
import contextlib
In [33]:
from astropy.io import fits
In [35]:
with fits.open('test.fits', mode='ostream') as f:
    pass
In [44]:
# Учебный пример

def parse_csv(file_path):
    x = []
    y = []
    with open(file_path) as fd:
        for line in fd:
            line = line.strip()
            x_, y_ = map(float, line.split(','))
            x.append(x_)
            y.append(y_)
    return x, y


x, y = parse_csv('diagram.csv')
plt.plot(x, y, 'x')
print(x)
[0.036950897, 0.03500012, 0.2093362, 0.25276598, 0.2743527, 0.27199373, 0.44834232, 0.50287396, 0.49865913, 0.6310321, 0.90020174, 0.9007238, 0.8012198, 0.89946604, 0.89804685, 1.0035977, 1.100515, 1.1055652, 1.3991741, 1.699523, 2.0069914, 1.9971569, 2.0035028, 1.9957709]
In [38]:
!head -n2 diagram.csv
0.036950897,-19.812012
0.03500012,32.59825
In [48]:
with open('diagram.csv', 'rb') as fd:
    text = fd.read()
text[:100]
Out[48]:
b'0.036950897,-19.812012\n0.03500012,32.59825\n0.2093362,42.75399\n0.25276598,9.813249\n0.2743527,-36.1325'
In [50]:
with open('cat.jpg', 'rb') as fd:
    data = fd.read(40)
data
Out[50]:
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xe2\x0cXICC_PROFILE\x00\x01\x01\x00\x00'
In [55]:
from PIL import Image
import numpy as np
In [57]:
with Image.open('cat.jpg') as img:
    a = np.array(img)
print(a)
a.dtype
[[[ 87 132  73]
  [ 90 128  67]
  [ 95 136  80]
  ...
  [ 83 125  49]
  [ 97 134  57]
  [101 135  61]]

 [[107 145  88]
  [107 138  70]
  [ 91 135  48]
  ...
  [ 76 123  43]
  [ 92 133  54]
  [104 145  67]]

 [[119 151  86]
  [119 149  79]
  [107 148  46]
  ...
  [ 74 123  44]
  [ 83 130  52]
  [ 81 129  55]]

 ...

 [[157 145 133]
  [142 146 129]
  [144 147 136]
  ...
  [180 159 164]
  [196 173 165]
  [113 101  89]]

 [[152 135 127]
  [111 108 119]
  [154 134 159]
  ...
  [121 102 106]
  [161 147 134]
  [210 193 183]]

 [[116 124 103]
  [ 86  90  93]
  [ 41  52  72]
  ...
  [161 160 156]
  [148 142 130]
  [191 173 153]]]
Out[57]:
dtype('uint8')
In [ ]: