TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login CONVERT LENGTH
login CONVERT TEMPERATURE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login WORK CALCULATOR
login CALCULATE LIFE

Python 3 programming language, learn python, tutorial

Previous articlePage bottomNext article  ALL TOPICS

Python 3. Module OS

 1.
import os
print ("1" , os.getcwd())

os.chdir("c:/windows")

print ("2" , os.getcwd())

try:
  os.chdir("c:/win")
except FileNotFoundError:
  print ("3" ,"There in no file")

# for linux
## os.fchmod(fd, mode)
## os.fchown(fd, uid, gid)
## os.chmod(path, mode)
## os.chown(path, uid, gid)
## os.link(source, link_name)

# for Linux. Create folders a/b/c/d/e/f/g/h/i/j
## os.mkdir(path[, mode])

os.chdir("c:/")

try:
  os.mkdir("./a")

except FileExistsError:
  print ("5", "Folder exists")

try:  
  os.symlink("./a", "./b")
  # not Possible

except NotImplementedError:
  print ("6", "os.symlink UNIX, LINUX command")

# Delete (dangerous)
## os.rmdir(path)
## os.unlink(path)

1a.
import os

path = "c:/"
dir  = "a"

# Executing a shell command
os.system("dir")    

# Get the users environment
d = dict(os.environ)
for i, j in d.items():
    print (i, " ", j)


# Returns the current working directory.
print(os.getcwd())

# Linux. Return the real group id of the current process.
# os.getgid()       

# Linux. Return the current process’s user id.
# os.getuid()    

# Returns the real process ID of the current process. OS name.
print ("4", "Operating system is:", os.name, "\nPID:", os.getpid())

# Linux. Set the current numeric umask and return the previous umask.
# os.umask(mask)   

# Linux.  Return information identifying the current operating system.
# os.uname()     

# Linux.  Change the root directory of the current process to path.
# os.chroot(path)   

# Return a list of the entries in the directory given by path.
print (os.listdir(path))

# Create a directory named path with numeric mode mode.

try:
    os.mkdir(path + dir)    
except:
    pass

try:
# Recursive directory creation function.
    os.makedirs(path + dir + "/" + dir)  
except:
    pass

# Remove (delete) the file path.
# os.remove(path)    

# Remove directories recursively.
# os.removedirs(path)

try:
# Rename the file or directory src to dst.
    os.rename(path + dir + "/" + dir, path + dir + "/" + "b")  
except:
    pass

# Remove (delete) the directory path.
# os.rmdir(path) 

2.
import os

def replaceText(file, search_for, replace_with):
    # replace strings in a text file

    back = os.path.splitext(file)[0] + ".bak"
    temp = os.path.splitext(file)[0] + ".tmp"

    try:
        # remove old temp file, if any
        os.remove(temp)
    except os.error:
        pass

    fi = open(file)
    fo = open(temp, "w")

    for s in fi.readlines():
        fo.write(s.replace(search_for, replace_with))

    fi.close()
    fo.close()

    try:
        # remove old backup file, if any
        os.remove(back)
    except os.error:
        pass

    # rename original to backup...
    os.rename(file, back)

    # ...and temporary to original
    os.rename(temp, file)

#
# try it out!

file = "sample.txt"

replaceText(file, "hello", "tjena")
replaceText(file, "tjena", "hello")

3. Files. (Task: show only txt files)
import os

for file in os.listdir("."):
    print (file)
-- 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
When a man has done his best, has given his all, and in the process supplied the needs of his family and his society, that man has made a habit of succeeding.
Mack R. Douglas
Being Challenged in Life is Inevitable, being Defeated is Optional.
Unknown
Temper your enjoyments with prudence, lest there be written on your heart that fearful word ''satiety.''
Francis Quarles