Python Scripting - Week 10 Flashcards
What are scripts?
Scripting refers to small programs that automate process
Abreviation of BASH ?
Born Again Shell
What we use for Client Side Web Script ?
JavaScript & VBScript, Front End Scripting
What we use for Servert Side Web Script
Python, .NET,ASP,JSP,PHP, Backend Scripting
Which libraries we use for working with paths ?
pathlib , os
How to create a path object ?
from pathlib import Path
p=Path(“D:/PythonCoding”)
print(p)
How to get path of current working directory ?
from pathlib import Path
p=Path.cwd()
print(p)
How to change present path ?
from pathlib import Path
p=Path(“D:/PythonCoding”)
print(p)
os.chdir(“D:/PythonCoding/Amrita University”)
print(Path.cwd())
How to print all files and directories in a given path ?
from pathlib import Path
p=Path(“D:/PythonCoding”)
for items in p.glob(“*”):
print(items)
How to print all PDF files in a given path ?
from pathlib import Path
p=Path(“D:/PythonCoding”)
for items in p.glob(“*.pdf”):
print(items)
How to print only files in a given path?
from pathlib import Path
p=Path(“D:/PythonCoding”)
for items in p.glob(“*”):
if items.is_file():
print(items) and exception
How to create a new directory in a given path ?
from pathlib import Path
dirPath = Path.cwd()
newDir=dirPath/”new”
newDir.mkdir()
Write a function to print only files in a directory by using OS library ?
def listAllFiles(str_):
from pathlib import Path
import os
fileList=[]
os.chdir(Path(str_))
for item in os.listdir():
if Path(item).is_file():
fileList.append(item)
return fileList
Write a function to print all directory in a directory by using OS library ?
def listAllFolders(str_):
from pathlib import Path
import os
folderList=[]
os.chdir(Path(str_))
for item in os.listdir():
if Path(item).is_dir():
folderList.append(item)
return folderList
How to convert path string to raw string?
Convert the string to raw string, Add r before the string.
sourceFile=Path(r”C:\Users\cs1\Documents\MEGAsync\PythonCoding\A4\shoping_list.pdf”)