Rajendra Dharmkar has Published 189 Articles

How to create file of particular size in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

5K+ 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

1K+ 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

14K+ 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

What are common practices for modifying Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 11-Dec-2019 09:59:06

317 Views

If you are modifying a module and want to test it in the interpreter without having to restart the shell everytime you save that module, you can use the reload(moduleName) function. reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for ... Read More

How to change the root directory of the current process in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 03-Oct-2019 06:21:11

1K+ Views

You can use the os.chroot to change the root directory of the current process to path. This command is only available on Unix systems. You can use it as follows:>>> import os >>> os.chroot('/tmp/my_folder')This changes the root directory of the script running to /tmp/my_folder.Read More

How can I get a file's permission mask using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 03-Oct-2019 05:57:13

306 Views

To get stat of a file, method stat() from the os module can be used. It performs a stat system call on the given path. For example, import os st = os.stat("file.dat")This function takes the name of a file, and returns a 10-member tuple with the following contents:(mode, ino, dev, ... Read More

How to generate XML documents with namespaces in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:36:28

2K+ Views

Currently you cannot add namespaces to XML documents directly as it is not yet supported in the in built Python xml package. So you will need to add namespace as a normal attribute to the tag. For example, import xml.dom.minidom doc = xml.dom.minidom.Document() element = doc.createElementNS('http://hello.world/ns', 'ex:el') element.setAttribute("xmlns:ex", "http://hello.world/ns") doc.appendChild(element) ... Read More

How to print to the Screen using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:34:57

805 Views

The basic way to do output to screen is to use the print statement.>>> print 'Hello, world' Hello, worldTo print multiple things on the same line separated by spaces, use commas between them. For example:>>> print 'Hello, ', 'World' Hello, WorldWhile neither string contained a space, a space was added ... Read More

Explain the visibility of global variables in imported modules in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:24:41

1K+ Views

Globals in Python are global to a module, not across all modules. (Unlike C, where a global is the same across all implementation files unless you explicitly make it static.). If you need truly global variables from imported modules, you can set those at an attribute of the module where ... Read More

Advertisements