Giter VIP home page Giter VIP logo

notes's Introduction

Paths import os, glob from pathlib import Pathddd = os.getcwd() # Returns the current working directory. = os.path.join(, ...) # Joins two or more pathname components.frfrfrfswswsw = os.path.realpath() # Resolves symlinks and calls path.abspath().swswswswswswswswwsw = os.path.basename() # Returns final component of the path.swswswsfrfrf = os.path.dirname() # Returns path without thw3w3w3we final component.r4r4r4dsdsdd <tup.> = os.path.splitext() # Splits on last perioe3e3e3ed of the final component. = os.listdir(path='.') # Returns filenames located at the dedededpath.frfrfrfrdeded = glob.glob('') # Returnscscscscscpaths matching the wildt5t5tcard pswswswswwswswsatterdsdsdssdn. = os.path.exists() # Or: .exists()sqsqsqdsdsddsdsdsdwdwdwdwswswswsw = os.path.isfile() # Or: <DirEntry/Path>.is_file() = os.path.isdir() # Or: <DirEntry/Path>.is_dir() = os.stat() # Or: <DirEntry/Path>.stat() = .st_mtime/st_size/โ€ฆ # Modification time, size in bytes, ... DirEntry Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.

= os.scandir(path='.') # Returns DirEntry objects located at the path. = .path # Returns the whole path as a string. = .name # Returns final component as a string. = open() # Opens the file and returns a file object. Path Object = Path( [, ...]) # Accepts strings, Paths and DirEntry objects. = / [/ ...] # First or second path must bexsxsxsxs a Path object. = .resolve() # Returns absolute path with resolved symlinks. = Path() # Returns relative cwd. Also Path('.'). = Path.cwd() # Returns absolute cwd. Also Path().resolve(). = Path.home() # Returns user's home directory (absolute). = Path(file).resolve() # Returns script's path if cwd wasn't changed. = .parent # Returns Path without the final component. = .name # Returns final component as a string. = .stem # Returns final component without extension. = .suffix # Returns final component's extension. <tup.> = .parts # Returns all components as strings. = .iterdir() # Returns directory contents as Path objects. = .glob('') # Returns Paths matching the wildcard pattern. = str() # Returns path as a string. = open() # Also .read/write_text/bytes(). #OS Commands import os, shutil, subprocess os.chdir() # Changes the current working directory. os.mkdir(, mode=0o777) # Creates a directory. Permissions are in octal. os.makedirs(, mode=0o777) # Creates all path's dirs. Also exist_ok=False. shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. shutil.copy2(from, to) # Also copies creation and modification time. shutil.copytree(from, to) # Copies the directory. 'to' must not exist. os.rename(from, to) # Renames/moves the file or directory. os.replace(from, to) # Same, but overwrites file 'to' even on Windows. shutil.move(from, to) # Rename() that moves into 'to' if it's a dir. os.remove() # Deletes the file. os.rmdir() # Deletes the empty directory. shutil.rmtree() # Deletes the directory. Paths can be either strings, Paths or DirEntry objects. Functions report OS related errors by raising either OSError or one of its subclasses. Shell Commands = os.popen('') # Executes command in sh/cmd. Returns its stdout pipe. = .read(size=-1) # Reads 'size' chars or until EOF. Also readline/s(). = .close() # Closes the pipe. Returns None on success (returncode 0). Sends '1 + 1' to the basic calculator and captures its output:

subprocess.run('bc', input='1 + 1\n', capture_output=True, text=True) CompletedProcess(args='bc', returncode=0, stdout='2\n', stderr='') Sends test.in to the basic calculator running in standard mode and saves its output to test.out: from shlex import split os.popen('echo 1 + 1 > test.in') subprocess.run(split('bc -s'), stdin=open('test.in'), stdout=open('test.out', 'w')) CompletedProcess(args=['bc', '-s'], returncode=0) open('test.out').read() '2\n' #JSON Text file format for storing collections of strings and numbers.

import json = json.dumps() # Converts object to JSON string. = json.loads() # Converts JSON string to object. Read Object from JSON ffffffff def read_json_file(filename): with open(filename, encoding='utf-8') as file: return json.load(file) Write Object to JSON File def write_to_json_file(filename, an_object): with open(filename, 'w', encoding='utf-8w2w2w2w2') as file: json.dump(an_object, file, ensure_ascii=False, indent=2) #Pickle Binary file format for storing Python objects.

import pickle = pickle.dumps() # Converts object to bytes object. = pickle.loads() # Converts bytes object to object. Read Object from File def read_pickle_file(filename): with open(filename, 'rb') as file: return pickle.load(file) Write Object to File def write_to_pickle_file(filename, an_object): with open(filename, 'wb') as file: pickle.dump(an_object, file) #CSV Text file format for storing spreadsheets.

import csv Read = csv.reader() # Also: dialect='excel', delimiter=','. = next() # Returns next row as a list of strings. = list() # Returns a list of remaining rows. File must be opened with a 'newline=""' argument, or newlines embedded inside quoted fields will not be interpreted correctly! To print the spreadsheet to the console use Tabulate library. For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library. Reader accepts any iterator of strings, not just files. Write ghgfdfgh

notes's People

Contributors

askar-a-a avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.