Rajendra Dharmkar has Published 452 Articles

How to create softlink of a file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 09:56:43

779 Views

The method os.symlink(src, dst) creates a symbolic link dst pointing to src. For example, if you have a file called photo.jpg and want to create a softlink/symbolic link to it called my_photo.jpg, then you could simply use:Example>>> import os >>> os.symlink('photo.jpg', 'my_photo.jpg')Now if you list the files in that directory, ... Read More

How to create hardlink of a file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 09:54:33

3K+ Views

The method os.link(src, dst) creates a hard link pointing to src named dst. This method is very useful to create a copy of existing file.ExampleFor example, if you have a file called photo.jpg and want to create a  hard link to it called my_photo.jpg, then you could simply use:>>> import ... Read More

How to force write of file with filedescriptor fd to disk using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 09:53:06

242 Views

You have to use the fdatasync(fd) function to force write of file with filedescriptor fd to disk. It does not force update of metadata. Also note that this is only available on Unix.A more cross platform solution would be to use fsync(fd) as it force write of file with filedescriptor ... Read More

How to change the user and group permissions for a directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 07:14:14

607 Views

You can change the owner of a file or a directory using the pwd, grp and os modules. The uid module is used to get the uid from user name, grp to get gid group name string and os to change the owner:Exampleimport pwd import grp import os uid = ... Read More

How to change the owner of a file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 07:09:55

1K+ Views

You can change the owner of a file or a directory using the pwd, grp and os modules. The uid module is used to get the uid from user name, grp to get gid group name string and os to change the owner:Exampleimport pwd import grp import os uid = ... Read More

How do we use file.readlines() to read multiple lines using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 08:05:44

5K+ Views

The read function reads the whole file at once. You can use the readlines function to read the file line by line.ExampleYou can use the following to read the file line by line:f = open('my_file.txt', 'r+') for line in f.readlines():     print line f.close()You can also use the with...open ... Read More

How to create file of particular size in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 08:04:51

4K+ Views

To create a file of a particular size, just seek to the byte number(size) you want to create the file of and write a byte there.For examplewith open('my_file', 'wb') as f:     f.seek(1024 * 1024 * 1024) # One GB     f.write('0')This creates a sparse file by not ... Read More

How to change current directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 07:34:07

803 Views

You can change directory or cd in Python using the os module. It takes as input the relative/absolute path of the directory you want to switch to.For example>>> import os >>> os.chdir('my_folder')

How can I do "cd" in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 07:33:08

11K+ Views

You can change directory or cd in Python using the os module. It takes as input the relative/absolute path of the directory you want to switch to.For example>>> import os >>> os.chdir('my_folder')

How to know current working directory in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 07:30:38

2K+ Views

To know the current working directory or pwd use the os module.For example>>> import os >>> print(os.getcwd()) /home/ayush/qna

Advertisements