Rajendra Dharmkar has Published 452 Articles

How to delete multiple files in a directory in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Dec-2019 07:05:10

2K+ Views

You can delete a single file or a single empty folder with functions in the os module.ExampleFor example, if you want to delete a file my_file.txt, >>> import os >>> os.remove('my_file.txt')The argument to os.remove must be absolute or relative path.To delete multiple files, just loop over your list of files ... Read More

What are common practices for modifying Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

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

195 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 do I disable log messages from the Requests Python module?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 11-Dec-2019 09:57:29

766 Views

You can disable logging from the requests module using the logging module.ExampleYou can configure it to not log messages unless they are at least warnings using the following code:import logging logging.getLogger("requests").setLevel(logging.WARNING)If you want to go a level higher and only want to log messages when they are errors or critical, ... Read More

How to call a function of a module from a string with the function's name in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 11-Dec-2019 09:25:32

787 Views

Objects in python have instance variables and methods as attributes. To call a function of a module from a string with the function's name in Python, you can get this attribute first and call the function object attached to it. For example, let's say that I have a module foo, ... 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

192 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 change the permission of a directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 03-Oct-2019 05:53:16

6K+ Views

On a platform with the chmod command available, you could call the chmod command like this:>>> import subprocess >>> subprocess.call(['chmod', '-R', '+w', 'my_folder'])If you want to use the os module, you'll have to recursively write it:Using os: import os def change_permissions_recursive(path, mode):     for root, dirs, files in os.walk(path, ... Read More

How to create a directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:53:04

467 Views

To create a directory, first check if it already exists using os.path.exists(directory). Then you can create it using:import os if not os.path.exists('my_folder'):     os.makedirs('my_folder')You can also use the python idiom EAFP: Easier to ask for forgiveness than permission. For example, import os try:     os.makedirs('my_folder') except OSError as ... 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

689 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

Advertisements