- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9735 Articles for Python

Updated on 13-Dec-2019 10:37:17
To use any package in your code, you must first make it accessible. You have to import it. You can't use anything in Python before it is defined. Some things are built in, for example the basic types (like int, float, etc) can be used whenever you want. But most things you will want to do will need a little more than that. For example, if you want to calculate cosine of 1 radian, if you run math.cos(0), you'll get a NameError as math is not defined.ExampleYou need to tell python to first import that module in your code so ... Read More 
Updated on 13-Dec-2019 10:31:33
"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files in binary mode, when specifying a mode, add 'b' to it.For examplef = open('my_file', 'w+b') byte_arr = [120, 3, 255, 0, 100] binary_format = bytearray(byte_arr) f.write(binary_format) f.close()This opens a file in binary write mode and writes the byte_arr array contents as bytes in the binary file, my_file.Read More 
Updated on 13-Dec-2019 10:34:18
To get the creation time of a file, you can use the os.path.getctime(file_path) on windows. On UNIX systems, you cant use the same function as it returns the last time that the file's attributes or content were changed. To get the creation time on UNIX based systems, use the st_birthtime attribute of the stat tuple.ExampleOn Windows:>>> import os >>> print os.path.getctime('my_file') 1505928271.0689342It gives the time in the number of seconds since the epoch. For UNIX systems, import os stat = os.stat(path_to_file) try: print(stat.st_birthtime) except AttributeError: # Probably on Linux. No easy way to get creation dates ... Read More 
Updated on 18-Feb-2020 04:59:41
To get the creation time of a file, you can use the os.path.getctime(file_path) on windows. On UNIX systems, you cant use the same function as it returns the last time that the file's attributes or content were changed. To get the creation time on UNIX based systems, use the st_birthtime attribute of the stat tuple. exampleOn Windows −>>> import os >>> print os.path.getctime('my_file') 1505928271.0689342It gives the time in the number of seconds since the epoch. For UNIX systems, import os stat = os.stat(path_to_file) try: print(stat.st_birthtime) except AttributeError: # Probably on Linux. No easy way to get creation ... Read More 
Updated on 20-Jun-2020 09:05:51
For this purpose let us use a dictionary object having digit as key and its word representation as value −dct={'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'Initializa a new string object newstr=''Using a for loop traverse each character ch from input string at check if it is a digit with the help of isdigit() function. If it is digit, use it as key and find corresponding value from dictionary and append it to newstr. If not append the character ch itself to newstr. Complete code is as follows:string='I have 3 Networking books, 0 Database books, and 8 Programming ... Read More 
Updated on 18-Feb-2020 04:57:20
You can delete a single file or a single empty folder with functions in the os module. For example, if you want to delete a file a.txt,>>> import os
>>> os.remove('a.txt')The argument to os.remove must be absolute or relative path. You can also use the os.unlink() remove files. For example,>>> import os
>>> os.unlink('a.txt') 
Updated on 18-Feb-2020 04:56:12
You can use the tempfile module to create a unique temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable and searchable only by the creating user ID. Note that the user of mkdtemp() is responsible for deleting the temporary directory when done with it. To create a new temporary directory, use it as follows −import tempfile _, temp_dir_path = tempfile.mkdtemp() # Do what you want with this directory # And remove the directory when doneNote that you need to manually delete this directory after you're done with ... Read More 
Updated on 18-Feb-2020 04:54:54
You can use the tempfile module to create a unique temporary file in the most secure manner possible. There are no race conditions in the file’s creation. The file is readable and writable only by the creating user ID. Note that the user of mkstemp() is responsible for deleting the temporary file when done with it. To create a new temporary file, use it as follows −import tempfile
_, temp_file_path = tempfile.mkstemp()
print("File path: " + temp_file_path)Note that you need to manually delete this file after you're done with it. 
Updated on 18-Feb-2020 04:54:05
You can use os.walk to recursively walk through the directory and subsequently use os.rename to rename the files.Exampleimport os def replace(folder_path, old, new): for path, subdirs, files in os.walk(folder_path): for name in files: if(old.lower() in name.lower()): file_path = os.path.join(path, name) new_name = os.path.join(path, name.lower().replace(old, new)) os.rename(file_path, new_name)You can use this function as follows −replace('my_folder', 'IMG', 'Image')This will find all files recursively within the folder ... Read More 
Updated on 13-Dec-2019 09:48:14
You can rename a directory in Python by moving it using the shutil module. The shutil.move(src, dst) moves the directory from src to dst. If you just change the name of the directory without specifying the path, you'll basically be renaming it.For example>>> import shutil
>>> shutil.move('my_folder', 'new_name')The above code renames my_folder to new_name. You could also use the os.rename(src, dst) for renaming directories.For example>>> import os
>>> os.rename('my_folder', 'new_name') Advertisements