Introduction to File Operations

In this section will learn how to read or write a file which is stored on your hard drive. Before we even open a file we have to find it on our hard drive. If you started Python from Command Prompt in Windows operating system or Terminal in Linux operating system there are two simple approaches how to open a folder where your specific file is stored and these are:
  1.       Navigate to a specific folder in CMD or Terminal before starting Python programming language or
  2.            Change working folder when you’re inside the Python

Navigate to specific folder from CMD or Terminal

This first approach is very easy. Commands are similar in CMD and Terminal and we’re going to show them step by step.

Navigate to specific folder in CMD on Windows

First search for cmd and you can open it as Administrator (if you have administrator rights) or you can open it as a user. The difference is:
-          If you open cmd as Administrator by right clicking on the ‘cmd’ icon and selecting ‘Run as Administrator when Command Prompt opens the starting address would probably be ‘C:\Windows\System32’
-          If you open cmd as classic user (without administrator rights) then your starting address would probably be C:\Users\username
The next thing to do is navigate to a specific folder using cd .. command to go back. For example
C:\Windows\System32> cd ..
C:\Windows>cd ..
C:\
To go further just type cd followed by name of the folder.
C:\Python27
To ease the navigation process through your hard drive you can use dir command and it will list files in your current folder.

Navigate to specific folder in Terminal

Procedure is similar to CMD on Windows operating system. After you open Terminal type
sudo –s
this command is necessary for administration rights. After you type this command and press Enter or Return button it will ask you for administrator password, type the password and press enter. Now you’ve gained administrator’s privileges. This command is optional for now but it will be necessary later in order to move, delete, copy or paste specific files.
To list what is in specific folder type ‘ls’. To navigate to specific folder type cd and the name of the folder and to go back just type cd .. .

Navigate to work folder in Python

The previous procedures in Windows and in Linux are very familiar to you but we’ve put them just in case. To navigate to a specific or work folder in Python first you need to do is import the os library and you can do this by typing the following command:
>>>import os
***Missing the os description
To check what is current Python work folder type
>>> os.getcwd()
'C:\\Users'
The getcwd is one of os method and to print out the current work folder you don’t need to give any arguments. To change the work folder just type os.chdir(directory adres)
'C:\\Users'
>>> os.chdir('C:\\Users\\username')
>>> os.getcwd()
'C:\\Users\\username'


Opening the file communicates with your operating system which knows where the data for each file is stored. When you open a file 

Nema komentara:

Objavi komentar