Saturday, May 22, 2021

Creating, removing directories, renaming files, different display formats

Script
#!/usr/local/bin/python3.5

import os
from datetime import datetime

# print(dir(os))
print(os.getcwd())
os.chdir('/Python-scripts/Memorable-Tests')
print(os.getcwd())
print(os.listdir())
os.mkdir('Victories-over-Australia')
os.makedirs('Victories-over-Australia/Eighties/In-Australia')
os.makedirs('Victories-over-Australia/Eighties/In-India')
os.makedirs('Victories-over-Australia/Nineties/In-Australia')
os.makedirs('Victories-over-Australia/Nineties/In-India')
os.makedirs('Victories-over-Australia/Twenty-first-century/In-Australia')
os.makedirs('Victories-over-Australia/Twenty-first-century/In-India')
os.removedirs('Wins-over-Zimbabwe/In-Zimbabwe')
os.removedirs('Wins-over-Zimbabwe/In-India')
os.rmdir('Triumphs-over-New-Zealand')
os.rename('Trent-Bridge-2007-K20.txt', 'Zaheer-Khan-Man-of-the-Match.txt')

print()
os.stat('Kolkata-2001-K10.txt')
print(os.stat('Kolkata-2001-K10.txt'))
print(os.stat('Kolkata-2001-K10.txt').st_size)
print(os.stat('Kolkata-2001-K10.txt').st_mtime)
modification_time = os.stat('Kolkata-2001-K10.txt').st_mtime
## The below line is possible because I have included the "from datetime import datetime" at the beginning of this program
print(datetime.fromtimestamp(modification_time))

## Entire directory tree
os.walk('/Python.scripts/Memorable-Tests')

for directory_path, directory_names, list_of_files in os.walk('/Python-scripts/Memorable-Tests'):
  print('Current path: ', directory_path)
  print('Directories: ', directory_names)
  print('List of files: ', list_of_files)
  print()

## Miscellaneous ways of checking and displaying file listings and directories
print(os.path.basename('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.dirname('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.split('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.exists('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.isdir('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.isfile('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))
print(os.path.splitext('/Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt'))

##print(dir(os.path))

Before the script execution

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr
total 12
-rw-r--r--. 1 root root    0 Feb 27 07:02 PortofSpain-1976-K02.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Chennai-2008-K15.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Oval-1971-K01.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Madras-1998-K08.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Lords-2014-K17.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Delhi-1999-K09.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Mumbai-2016-K19.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Kanpur-1996-K07.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Adelaide-2003-K12.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Mohali-2010-K16.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Lords-1986-K04.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Kolkata-2005-K13.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Melbourne-1977-K03.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Calcutta-1993-K05.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Perth-2008-K14.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Headingley-2002-K11.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Vishakapatnam-2016-K18.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Ahmedabad-1996-K06.txt
-rw-r--r--. 1 root root  629 Feb 28 03:44 Kolkata-2001-K10.txt
-rw-r--r--. 1 root root    0 Feb 28 03:45 Trent-Bridge-2007-K20.txt
drwxr-xr-x. 4 root root   41 Feb 28 03:59 Wins-over-Zimbabwe
drwxr-xr-x. 2 root root    6 Feb 28 04:00 Triumphs-over-New-Zealand
drwxr-xr-x. 4 root root 4096 Feb 28 04:00 .
drwxr-xr-x. 3 root root 4096 Feb 28 04:00 ..
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Triumphs-over-New-Zealand/
total 4
drwxr-xr-x. 4 root root 4096 Feb 28 04:00 ..
drwxr-xr-x. 2 root root    6 Feb 28 04:00 .
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Wins-over-Zimbabwe/
total 4
drwxr-xr-x. 2 root root    6 Feb 28 03:59 In-India
drwxr-xr-x. 2 root root    6 Feb 28 03:59 In-Zimbabwe
drwxr-xr-x. 4 root root   41 Feb 28 03:59 .
drwxr-xr-x. 4 root root 4096 Feb 28 04:00 ..
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >


Execution

/Python-scripts
/Python-scripts/Memorable-Tests
['Vishakapatnam-2016-K18.txt', 'Madras-1998-K08.txt', 'Chennai-2008-K15.txt', 'Oval-1971-K01.txt', 'Kanpur-1996-K07.txt', 'Mohali-2010-K16.txt', 'Mumbai-2016-K19.txt', 'Kolkata-2005-K13.txt', 'Melbourne-1977-K03.txt', 'Lords-1986-K04.txt', 'PortofSpain-1976-K02.txt', 'Lords-2014-K17.txt', 'Headingley-2002-K11.txt', 'Ahmedabad-1996-K06.txt', 'Perth-2008-K14.txt', 'Adelaide-2003-K12.txt', 'Calcutta-1993-K05.txt', 'Delhi-1999-K09.txt', 'Kolkata-2001-K10.txt', 'Trent-Bridge-2007-K20.txt', 'Wins-over-Zimbabwe', 'Triumphs-over-New-Zealand']

os.stat_result(st_mode=33188, st_ino=5039151, st_dev=51714, st_nlink=1, st_uid=0, st_gid=0, st_size=629, st_atime=1488253486, st_mtime=1488253477, st_ctime=1488253477)
629
1488253477.7849207
2017-02-28 03:44:37.784921
Current path:  /Python-scripts/Memorable-Tests
Directories:  ['Victories-over-Australia']
List of files:  ['Vishakapatnam-2016-K18.txt', 'Madras-1998-K08.txt', 'Chennai-2008-K15.txt', 'Oval-1971-K01.txt', 'Kanpur-1996-K07.txt', 'Mohali-2010-K16.txt', 'Mumbai-2016-K19.txt', 'Kolkata-2005-K13.txt', 'Melbourne-1977-K03.txt', 'Lords-1986-K04.txt', 'PortofSpain-1976-K02.txt', 'Lords-2014-K17.txt', 'Headingley-2002-K11.txt', 'Ahmedabad-1996-K06.txt', 'Perth-2008-K14.txt', 'Adelaide-2003-K12.txt', 'Calcutta-1993-K05.txt', 'Delhi-1999-K09.txt', 'Kolkata-2001-K10.txt', 'Zaheer-Khan-Man-of-the-Match.txt']

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia
Directories:  ['Eighties', 'Nineties', 'Twenty-first-century']
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Eighties
Directories:  ['In-Australia', 'In-India']
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Eighties/In-Australia
Directories:  []
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Eighties/In-India
Directories:  []
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Nineties
Directories:  ['In-Australia', 'In-India']
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Nineties/In-Australia
Directories:  []
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Nineties/In-India
Directories:  []
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Twenty-first-century
Directories:  ['In-Australia', 'In-India']
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Twenty-first-century/In-Australia
Directories:  []
List of files:  []

Current path:  /Python-scripts/Memorable-Tests/Victories-over-Australia/Twenty-first-century/In-India
Directories:  []
List of files:  []

Kolkata-2001-K10.txt
/Python-scripts/Memorable-Tests
('/Python-scripts/Memorable-Tests', 'Kolkata-2001-K10.txt')
True
False
True
('/Python-scripts/Memorable-Tests/Kolkata-2001-K10', '.txt')



After the execution of the script
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr
total 12
-rw-r--r--. 1 root root    0 Feb 27 07:02 PortofSpain-1976-K02.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Chennai-2008-K15.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Oval-1971-K01.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Madras-1998-K08.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Lords-2014-K17.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Delhi-1999-K09.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Mumbai-2016-K19.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Kanpur-1996-K07.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Adelaide-2003-K12.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Mohali-2010-K16.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Lords-1986-K04.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Kolkata-2005-K13.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Melbourne-1977-K03.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Calcutta-1993-K05.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Perth-2008-K14.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Headingley-2002-K11.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Vishakapatnam-2016-K18.txt
-rw-r--r--. 1 root root    0 Feb 27 07:02 Ahmedabad-1996-K06.txt
-rw-r--r--. 1 root root  629 Feb 28 03:44 Kolkata-2001-K10.txt
-rw-r--r--. 1 root root    0 Feb 28 03:45 Zaheer-Khan-Man-of-the-Match.txt
drwxr-xr-x. 3 root root 4096 Feb 28 04:09 ..
drwxr-xr-x. 5 root root   66 Feb 28 04:09 Victories-over-Australia
drwxr-xr-x. 3 root root 4096 Feb 28 04:09 .
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > cd Victories-over-Australia/
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests/Victories-over-Australia >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests/Victories-over-Australia > ls -altr
total 4
drwxr-xr-x. 4 root root   42 Feb 28 04:09 Twenty-first-century
drwxr-xr-x. 4 root root   42 Feb 28 04:09 Nineties
drwxr-xr-x. 4 root root   42 Feb 28 04:09 Eighties
drwxr-xr-x. 3 root root 4096 Feb 28 04:09 ..
drwxr-xr-x. 5 root root   66 Feb 28 04:09 .
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests/Victories-over-Australia >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests/Victories-over-Australia >



The directory "Victories-over-Australia" got created as well as some directories below it.

The file "Trent-Bridge-2007-K20.txt" got renamed to "Zaheer-Khan-Man-of-the-Match.txt" .

Notice that the two sub-directories inside "Wins-over-Zimbabwe" i.e. "In-India" and "In-Zimbabwe" are gone.  And the parent directory "Wins-over-Zimbabwe" is gone as well.

Also notice that the directory "Triumphs-over-New-Zealand" is gone.








No comments:

Post a Comment