Next, read a file using the read() method. For example, to open and read: fp = open('sample.txt', 'r')
Sys.executablecode contains full path of the currently running Python interpreter.
How to find full path of the Python interpreter import inspect import osįilename = inspect. If you want to cover the case that a script is called via execfile(), you can use the inspect module to access the current filename and the path. To get the directory containing current script file: import pathlib print(pathlib. In Python 3.4+, you can use pathlib module to get the executing file path. Output: D:\Python_projects\demo\scripts\1.pyĢ. The _file_ attribute can help you find out where the file you are executing is located. To get the executing file path, use os module. The current file path is where the file is executing.ġ. In Python 3.4+, you can use pathlib module. To get the current working directory, use os module.
The current directory is when you run the Python script.ġ. How to get current directory and file path in Python Resize an image and keep its aspect ratio.Calculate Euclidean distance btween two points.Python ImportError and ModuleNotFoundError.Indefinitely Request User Input Until a Valid Response.Test Single Variable in Multiple Values.Create variables dynamically in while loop.Python Global Variables and Global Keyword.Filter dictionary to contain specific keys.Get key with maximum value in dictionary.Create dictionary with dict comprehension and iterables.Copy a dictionary and edit the copy only.Convert a String representation of a Dictionary to a dictionary.Extract specific element from each sublist.Find the intersection between two lists.Find the cumulative sum of numbers in a list.
Get the Cartesian product of a series of lists.Get all possible combinations of a list's elements.Creare a flat list out of a nested list.Sort a list of objects by an attribute of the objects.Sort list based on values from another list.Convert string representation of list to a list.Check if string exists in a list of strings.Check if multiple strings exist in another string.Conbine items in a list to a single string.Split strings into words with multiple delimiters.String updating, replacing and deleting.Read specific lines from a file by line number.Convert between local time and UTC time.strftime(): from datetime to readable string.