Python (2.7.6) File Checking - python

I've got a problem that has been troubling me for nearly a week. It was an exercise given out in class (not as homework) for us to try and find the answer to. The question is:
"Write a program that prompts the user to enter the name of a file, and then determines whether or not that file exists. The program should not alter the contents of the file (if it does exist) in any way.
For simplicity, check only the directory/folder in which your program exists (so do not worry about drives, directory path names and so on).
Hint: A promising way to do this would be to attempt to open the file and observe results."
I've created a file called 'hello.txt' and put it in a seperate folder called 'Test Folder' and tried to get it to work so many times but I'm getting no where. Any ideas?

#!/usr/bin/python
import sys
import os.path
fileName = sys.argv[1]
print(os.path.isfile(fileName))
From this question: How do I check whether a file exists using Python?

The first thing to do here is decompose the requirements and deal with each separately.
Prompt the user for a filename
Determine whether "that file" exists.
Implicitly - report the answer to 2.
You need to be able to do both (3 should be obvious), and realize that by "that file exists", what is really meant is whether a file with that name exists.
Try either of the two, or both by running the python interpreter. If you still need help, show what you have done.

Related

Python finds path to a file, yet says the file doesn't exist when I check for it?

I'm writing code for a school task which wants me iterate through 1000 .txt files and locate numbers in them and add them to a list. Repeated numbers should not be added. I had some issues with the iterations, with python saying the files doesn't exist, so I wanted to test it in a singular instance with one file:
print(os.path.abspath('number_000.txt'))
which returned the path as expected.
Yet when I try:
print(os.path.exists('number_000.txt'))
It returns False, which doesn't make sense to me. Can anyone explain?

entering files path using input()

I'm trying to extract information from excel file, and eventually put the values from the excel in a docx file. when I wrote the code I entered a specific path using the syntax (r"file path), and I had no problem. because the program was created for a friend of mine, and it will run on a different computer, I am looking for a way for my friend to open the exact excel file he wanted. down below you can see some codes. thanks in advance to anybody that spend some time trying to solve it!
this one worked for me:
loc = (r"C:\Users\dddor\Desktop\python24\report_example.xlsx")
this one brings error:
loc=(input('enter file location: '))
I also tried, but same error poped:
loc=("r"+input('enter file location: '))
Even when I copy the file path, that worked for me (both with and without the "r" it doesn't work)
you can use pathlib
This module offers classes representing filesystem paths with semantics appropriate for different operating systems
from pathlib import Path:
file_path = Path(input('enter file location: '))

How to take arbitrary path in directory to see if file exists in python

I want to take an arbitrary path to a file to see if it exists inside of a file path. I know this file does exist.
string = r'pathos/.*_cube/uni/ToolSub.pm'
result = os.path.isfile(string2)
print (result)
The directory of .*_cube is 'llb_cube" for me. However it can vary for each person testing the code for one person it may be "gfs_cube" and another 'hgf_cube" therefore I want to keep this directory arbitrary.
My problem is I cannot find a way for my program to comprehend that I want it to read an arbitrary string prefix at _cube (allow any characters before _cube). It keeps saying it does not exist but when I hard code the file to
string = r'pathos/llb_cube/uni/ToolSub.pm'
It returns true and it exists.

How to have multiple programs access the same file without manually giving them all the file path?

I'm writing several related python programs that need to access the same file however, this file will be updated/replaced intermittently and I need them all to access the new file. My current idea is to have a specific folder where the latest file is placed whenever it needs to be replaced and was curious how I could have python select whatever text file is in the folder.
Or, would I be better off creating a program that has a Class entirely dedicated to holding the information of the file and have each program reference the file in that class. I could have the Class use tkinter.filedialog to select a new file whenever necessary and perhaps have a text file that has the path or name to the file that I need to access and have the other programs reference that.
Edit: I don't need to write to the file at all just read from it. However, I would like to have it so that I do not need to manually update the path to the file every time I run the program or update the file path.
Edit2: Changed title to suit the question more
If the requirement is to get the most recently modified file in a specific directory:
import os
mypath = r'C:\path\to\wherever'
myfiles = [(f,os.stat(os.path.join(mypath,f)).st_mtime) for f in os.listdir(mypath)]
mysortedfiles = sorted(myfiles,key=lambda x: x[1],reverse=True)
print('Most recently updated: %s'%mysortedfiles[0][0])
Basically, get a list of files in the directory, together with their modified time as a list of tuples, sort on modified date, then get the one you want.
It sounds like you're looking for a singleton pattern, which is a neat way of hiding a lot of logic into an 'only one instance' object.
This means the logic for identifying, retrieving, and delivering the file is all in one place, and your programs interact with it by saying 'give me the one instance of that thing'. If you need to alter how it identifies, retrieves, or delivers what that one thing is, you can keep that hidden.
It's worth noting that the singleton pattern can be considered an antipattern as it's a form of global state, it depends on the context of the program if this is a deal breaker or not.
To "have python select whatever text file is in the folder", you could use the glob library to get a list of file(s) in the directory, see: https://docs.python.org/2/library/glob.html
You can also use os.listdir() to list all of the files in a directory, without matching pattern names.
Then, open() and read() whatever file or files you find in that directory.

Check if an open file has been deleted after open in python

Is it possible to check if a file has been deleted or recreated in python?
For example, if you did a open("file") in the script, and then while that file is still open, you do rm file; touch file;, then the script will still hold a reference to the old file even though it's already been deleted.
You should fstat the file descriptor for the opened file.
>>> import os
>>> f = open("testdv.py")
>>> os.fstat(f.fileno())
posix.stat_result(st_mode=33188, st_ino=1508053, st_dev=65027L, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1107, st_atime=1349180541, st_mtime=1349180540, st_ctime=1349180540)
>>> os.fstat(f.fileno()).st_nlink
1
Ok, this file has one link, so one name in the filesystem. Now remove it:
>>> os.unlink("testdv.py")
>>> os.fstat(f.fileno()).st_nlink
0
No more links, so we have an "anonymous file" that's only kept alive as long as we have it open. Creating a new file with the same name has no effect on the old file:
>>> g = open("testdv.py", "w")
>>> os.fstat(g.fileno()).st_nlink
1
>>> os.fstat(f.fileno()).st_nlink
0
Of course, st_nlink can sometimes be >1 initially, so checking that for zero is not entirely reliable (though in a controlled setting, it might be good enough). Instead, you can verify whether the file at the path you initially opened is the same one that you have a file descriptor for by comparing stat results:
>>> os.stat("testdv.py") == os.fstat(f.fileno())
False
>>> os.stat("testdv.py") == os.fstat(g.fileno())
True
(And if you want this to be 100% correct, then you should compare only the st_dev and st_ino fields on stat results, since the other fields and st_atime in particular might change in between the calls.)
Yes. Use the os.stat() function to check the file length. If the length is zero (or the function returns the error "File not found"), then someone deleted the file.
Alternatively, you can open+write+close the file each time you need to write something into it. The drawback is that opening a file is a pretty slow operation, so this is out of the question if you need to write a lot of data.
Why? Because the new file isn't the file that you're holding open. In a nutshell, Unix filesystems have two levels. One is the directory entry (i.e. the file name, file size, modification time, pointer to the data) and the second level is the file data.
When you open a file, Unix uses the name to find the file data. After that, it operates only on the second level - changes to the directory entry have no effect on any open "file handles". Which is exactly why you can delete the directory entry: Your program isn't using it.
When you use os.stat(), you don't look at the file data but at the directory entry again.
On the positive side, this allows you to create files which no one can see but your program: Open the file, delete it and then use it. Since there is no directory entry for the file, no other program can access the data.
On the negative side, you can't easily solve problems like the one you have.
Yes -- you can use the inotify facility to check for file changes and more. There also is a Python binding for it. Using inotify you can watch files or directories for filesystem activiy. From the manual the following events can be detected:
IN_ACCESS File was accessed (read) (*).
IN_ATTRIB Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*).
IN_CLOSE_WRITE File opened for writing was closed (*).
IN_CLOSE_NOWRITE File not opened for writing was closed (*).
IN_CREATE File/directory created in watched directory (*).
IN_DELETE File/directory deleted from watched directory (*).
IN_DELETE_SELF Watched file/directory was itself deleted.
IN_MODIFY File was modified (*).
IN_MOVE_SELF Watched file/directory was itself moved.
IN_MOVED_FROM File moved out of watched directory (*).
IN_MOVED_TO File moved into watched directory (*).
IN_OPEN File was opened (*).
From here you can google yourself a solution, but I think you get the overall idea. Of course this may only work on Linux, but from your question I assume you are using it (references to rm and touch).

Categories