What is the time complexity of Python's os.path.exists()? [duplicate] - python

This question already has answers here:
python: complexity of os.path.exists with a ext4 filesystem?
(2 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have a ton of folders nested inside one another.
What is time complexity of Python's os.path.exists() ?
Does it change if used with different OS ?

os.path.exists just performs a system call and returns True if the path points to an existing file or directory.
Python seems to performa an lstat() system call on the given path.
If the operation only consists in a lookup in a hash table, then the cost is O(1), but it may depend on the Operating System and in how is it implemented internally.

Given you are asking the OS if a single file exists, it does not need to do any algorithmic logic, or go down your path... I don't see how it could be anything else than O(1).

Related

How to set creation times in Linux? [duplicate]

This question already has an answer here:
How can I set file creation times in ZFS?
(1 answer)
Closed 12 months ago.
utime only modifies accessed and modified times. Is there a way to do modify the creation/birth time with Python?
Filesystem is ZFS if that matters.
You want touch functionality, and it's available in the os module using utime().
See https://docs.python.org/3.8/library/os.html#os.utime, and also
Implement touch using Python?

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

20,000 letter long dictionary in one line in Python [duplicate]

This question already has answers here:
What is the max length of a Python string?
(3 answers)
Closed 4 years ago.
do you have any experience with such a long dictionaries in one line 20.000? I am working on a module that has such. First I imported this dictionary with json, but I feel like it's better to import as least as possible. The code is not meant to be ever opened by the users and is working well for now. Do you have any experience with line that has 20.000 characters? can it cause any problems in the future?
Thanks
One practical problem you could run into is LINE_MAX. On *nix systems, that's the maximum length of a line which is guaranteed to be supported by all the usual utilities like grep and so on. The value promised by POSIX is 2048 bytes, but some systems have a larger value like 4096.
So, if you do decide to have 20,000 characters on a single line, you can't expect the usual utility programs to be able to operate on that file.
Ref: https://www.gnu.org/software/libc/manual/html_node/Utility-Minimums.html

Different ways of closing files in Python [duplicate]

This question already has answers here:
Is there a need to close files that have no reference to them?
(6 answers)
Closed 4 years ago.
Usually it is required to call the .close() method on a file object or use the "with open" construct to make sure it gets closed.
However, I am wondering if writing to a file like this leaves the file closed:
open(path,'w').writelines(fileContents)
No, open(path,'w').writelines(fileContents) does not close the file. It's left in an open state, even though the file object is anonymous and it's not assigned to anything. CPython will clean up the handle if it goes out of scope.
Furthermore, I believe garbage collection would have to occur, which may or may not happen at the end of a block (it usually does, but there's no mandate or guarantee AFAIK). Other Python interpreters may or may not close the file when it goes out of scope, so don't rely on that mechanism for general Python code.
It's best to just use the with idiom and be done with it. There are very few reasons not to use with when dealing with file descriptors.

Do files automatically close if I don't assign them to a variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is close() necessary when using iterator on a Python file object
for line in open("processes.txt").readlines():
doSomethingWith(line)
Take that code for example. There's nothing to call close() on. So does it close itself automatically?
Files will close when the corresponding object is deallocated. The sample you give depends on that; there is no reference to the object, so the object will be removed and the file will be closed.
Important to note is that there isn't a guarantee made as to when the object will be removed. With CPython, you have reference counting as the basis of memory management, so you would expect the file to close immediately. In, say, Jython, the garbage collector is not guaranteed to run at any particular time (or even at all), so you shouldn't count on the file being closed and should instead close the file manually or (better) use a with statement.
AFAIK they don't. In order to have autoclosing, you need to use a context manager, such as with
Although the object itself may be reclaimed by garbage collection and closed, there is no definite time to when garbage collection occurs.
with open("processes.txt") as openfile:
<do stuff>

Categories