Folders and Files¶
Let us quickly recap details about folders and files, especially using Linux. You need to be comfortable with the following.
- Differentiating Files and Folders. Keep in mind that Folders and Directories means the samething.
- Understanding Absolute or Fully Qualified Path.
- Understanding Relative Path.
- Understanding File or Folder permissions.
In [1]:
# Listing files in the present working directory
!ls -ltr
total 88 drwxrwxr-x 2 itversity itversity 111 Mar 8 02:04 data -rw-rw-r-- 1 itversity itversity 4730 Mar 8 02:04 12_managing_files_and_folders_using_python.ipynb -rw-rw-r-- 1 itversity itversity 10177 Mar 8 02:04 11_write_strings_to_file_in_append_mode.ipynb -rw-rw-r-- 1 itversity itversity 5141 Mar 8 02:04 10_read_csv_into_list_of_strings.ipynb -rw-rw-r-- 1 itversity itversity 3584 Mar 8 02:04 09_overview_of_delimited_strings.ipynb -rw-rw-r-- 1 itversity itversity 8830 Mar 8 02:04 08_overview_of_modes_to_write_into_files.ipynb -rw-rw-r-- 1 itversity itversity 4943 Mar 8 02:04 07_write_string_to_text_file.ipynb -rw-rw-r-- 1 itversity itversity 4474 Mar 8 02:04 06_read_text_file_into_string.ipynb -rw-rw-r-- 1 itversity itversity 4445 Mar 8 02:04 05_overview_of_retail_data.ipynb -rw-rw-r-- 1 itversity itversity 2718 Mar 8 02:04 04_file_paths_and_names.ipynb -rw-rw-r-- 1 itversity itversity 5533 Mar 8 02:04 03_folders_and_files.ipynb -rw-rw-r-- 1 itversity itversity 1698 Mar 8 02:04 02_overview_of_file_io.ipynb -rw-rw-r-- 1 itversity itversity 1375 Mar 8 02:04 01_basics_of_file_io_using_python.ipynb
In [2]:
# Listing files and folders under /data/retail_db
!ls -ltr /data/retail_db
total 20128 drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 categories drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 customers -rw-rw-r-- 1 itversity itversity 1748 Mar 8 02:04 create_db_tables_pg.sql -rw-rw-r-- 1 itversity itversity 10303297 Mar 8 02:04 create_db.sql drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 departments drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 order_items -rw-rw-r-- 1 itversity itversity 10297372 Mar 8 02:04 load_db_tables_pg.sql drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 orders drwxrwxr-x 2 itversity itversity 24 Mar 8 02:04 products
Absolute and Relative Paths¶
Let us get into the details related to accessing the files either by using absolute as well as relative paths.
- In Linux, absolute path typically starts with
/
. For example the absolute path ofretail_db
which we have used above is/data/retail_db
. - Now I would like to perform this task to understand about relative path. You can access files using relative paths.
- Open Terminal
- Run
cd /data/retail_db
. You are using absolute path ofretail_db
here to change to that directory. - Run
ls -ltr
. You should see the files and folders. The entries which start withd
are folders or directories. - Now run
cd orders
. You are using relative path of orders to get into the folder/data/retail_db/orders
. Asorders
is the subfolder or subdirectory under/data/retail_db
, you will be able to get into it with out any issues. - We can also represent the directory which we are in using
.
and parent directory using..
. - If you look at the output of
ls -ltr
which was run earlier, you can see other folders as well along withorders
.order_items
is one of them. - Now you are in
/data/retail_db/orders
and/data/retail_db
also haveorder_items
. You need to runcd ../order_items
to get into theorder_items
folder using relative path. - You can also use absolute path from any where any time. For example from any folder you can run
cd /data/retail_db/products
to get into products folder.
- Here are the commands for your reference. You can run these using terminal.
cd /data/retail_db
ls -ltr
cd orders # using relative path from /data/retail_db to get into orders
pwd
cd ../order_items # using relative path from orders to get into order_items
pwd
cd /data/retail_db/products
pwd
cd # Switch to home directory
pwd
cd /data/retail_db/products # We can use absolute path from any location
{note}
You should be really comfortable with terms file, directory, absolute path and relative path by now.
In [3]:
!pwd
/home/itversity/itversity-material/01-python-and-sql/20_basics_of_file_io_using_python
In [4]:
!ls -ltr
total 88 drwxrwxr-x 2 itversity itversity 111 Mar 8 02:04 data -rw-rw-r-- 1 itversity itversity 4730 Mar 8 02:04 12_managing_files_and_folders_using_python.ipynb -rw-rw-r-- 1 itversity itversity 10177 Mar 8 02:04 11_write_strings_to_file_in_append_mode.ipynb -rw-rw-r-- 1 itversity itversity 5141 Mar 8 02:04 10_read_csv_into_list_of_strings.ipynb -rw-rw-r-- 1 itversity itversity 3584 Mar 8 02:04 09_overview_of_delimited_strings.ipynb -rw-rw-r-- 1 itversity itversity 8830 Mar 8 02:04 08_overview_of_modes_to_write_into_files.ipynb -rw-rw-r-- 1 itversity itversity 4943 Mar 8 02:04 07_write_string_to_text_file.ipynb -rw-rw-r-- 1 itversity itversity 4474 Mar 8 02:04 06_read_text_file_into_string.ipynb -rw-rw-r-- 1 itversity itversity 4445 Mar 8 02:04 05_overview_of_retail_data.ipynb -rw-rw-r-- 1 itversity itversity 2718 Mar 8 02:04 04_file_paths_and_names.ipynb -rw-rw-r-- 1 itversity itversity 5533 Mar 8 02:04 03_folders_and_files.ipynb -rw-rw-r-- 1 itversity itversity 1698 Mar 8 02:04 02_overview_of_file_io.ipynb -rw-rw-r-- 1 itversity itversity 1375 Mar 8 02:04 01_basics_of_file_io_using_python.ipynb
In [5]:
# Listing files in the home directory. ~ represents home directory. It is a key on the keyboard.
# You can find the ~ key on the left side of 1 in most of the keyboards.
!ls -ltr ~
total 0 drwxrwxr-x 5 itversity itversity 91 Mar 8 02:04 itversity-material
- The lines which start with
d
represents folders or directories. - The lines which start with
-
represents files.
Let us go through the one sample in detail and understand what they actually mean.
drwxr-xr-x 5 itversity itversity 4096 Jul 13 07:21 itversity-material
drwxr-xr-x
– Permissions of the file or folder.5
– ignore for now- First
itversity
– Owner - Second
itversity
– Group 4096
– Size of file or folderJul 13 07:21
– Last modified timeitversity-material
– File name or folder namerwx
afterd
tells thatitversity
who is owner over this file or folder haveread
,write
andexecute
permissions.- I am currently logged in as
itversity
and hence I will be able to performread
,write
andexecute
operations on the directoryitversity-material
.