I want to run some image processing code on a Linux server.
The code is located in
/q/w/e/r/t
The images are located in
/abc/d/e/f/g # the images are in the g folder
This is the code I'm using:
path = "/abc/d/e/f"
new_path = os.path.join(path, 'g', '001.png')
img1= cv2.imread(new_path)
However, this is the error I'm getting:
[ WARN:0#"a number"] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('/abc/d/e/f/g/001.png'): can't open/read file: check file path/integrity
How can I solve the problem?
Probably it's because of permissions, but to reveal what is happening exactly best is to run the program with strace: https://man7.org/linux/man-pages/man1/strace.1.html which will show you the exact problem while it's trying to access to the file.
You can try the following:
Check whether you have permission to read and write the file. You can simply go to directory that contains all subdirectories and give read and write access to all subdirectories to all users.
Check whether you can access the directory stored in variable path.
Check the spelling of your pathname
Use try and catch to open the files - this way you see whether the problem is only one file or all of them which narrows the source of bug down.
Check the start of your path. Is that correct and does your program know the path?
If you can, download an image and have a look at it. Is it corrupted?
The OpenCV error sounds like certain library paths are not set. Either OpenCV is not properly installed or the path to the library not set. Might be worth to reinstall and set the paths properly.
Check that...
the path is indeed correct, that is, /abc/d/e/f/g/001.png exists
the process executing the python script has the necessary permissions to read this file
the file isn't corrupt (copy it to your local computer and open it in a browser/image viewer) (After reading the opencv source that's probably not it)
You can also "walk" the filesystem so you get all files below a certain directory:
def get_files(path=os.getcwd()):
files = []
if not path.endswith(os.sep):
path += os.sep
for root, dirs, files in os.walk(path)
for file in files:
filepath = os.path.join(root, file)
files.append(filepath)
return files
files = get_files("/abc/d/e/f")
Also, check your file and path. On the linux system's shell (I assume it is Bash):
$ ls -l <file> # check if it exists and whether the permissions are correct
$ file <file> # check the filetype (should hint at being an image)
Related
My work needs me to collect some file names and their generating time.
I am using the fileName = tkinter.filedialog.askopenfilenames() to realize the function, that the program pops up a window to ask for selecting files, then I can get the files' pathes and then use fileGeneratedTime = datetime.datetime.fromtimestamp(os.path.getmtime(fileName)) to get the files' generating time.
But now here comes the problem. When I want to get the path of a .lnk file, however, it returns the path of the file which the .lnk file is pointing to. It is OK to run the program on the origin computer that has the .lnk files, but when I copy the .lnk files to other computers, the program says FileNotFoundError.
So, is there any parameters that can make the fileName = tkinter.filedialog.askopenfilenames() returns the .lnk file itself's path (not the path of the file which the .lnk file points to)? Or is there any other ways to realize the same function?
Thanks for your answering!
I am getting this error sometimes when working on mac in Processing for Python. Seemingly for no reason, sometimes the current working directory becomes what you see in the image while other times it is the working directory of the folder the pyde file is in as it should be.
Any ideas on why this is occurring?
It's to avoid problems like these that I always try to use absolute paths. I would suggest you try something like this for file paths:
import os
# This will be the path to your .py file
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
# This will be the path to your text file, if it is in the same directory as the .py
LEVELS_FILE_PATH = os.path.join(FILE_PATH, "levels.txt")
Then, instead of your current open statement you could have:
f = open(LEVELS_FILE_PATH, 'r')
My code below, Python can't read my file:
f = open('./resource/review.txt','r')
The error is as in photo: Errno 2: No such file in directory.
You can try \ instead of /
f = open('.\\resource\\review.txt','r')
content = f.read()
print(content)
Because the file is not located at the same folder as your script you need to go back one folder.
In order to do that tou need '..' (double dot) and not one, this will indicate to go back a folder relative to the current path.
The message Errno 2: No such file in directory simply means that your relative pathname to your file cannot be found in the directory in which you're executing (which might not be the directory the Python script is in or that you think you're in, if you're using an IDE).
So to see what directory your Python is actually running in, do:
print(f'current directory is: {os.getcwd()}')
I am working on the listener portion of a backdoor program (for an ETHICAL hacking course) and I would like to be able to read files from any part of my linux system and not just from within the directory where my listener python script is located - however, this has not proven to be as simple as specifying a typical absolute path such as "~/Desktop/test.txt"
So far my code is able to read files and upload them to the virtual machine where my reverse backdoor script is actively running. But this is only when I read and upload files that are in the same directory as my listener script (aptly named listener.py). Code shown below.
def read_file(self, path):
with open(path, "rb") as file:
return base64.b64encode(file.read())
As I've mentioned previously, the above function only works if I try to open and read a file that is in the same directory as the script that the above code belongs to, meaning that path in the above content is a simple file name such as "picture.jpg"
I would like to be able to read a file from any part of my filesystem while maintaining the same functionality.
For example, I would love to be able to specify "~/Desktop/another_picture.jpg" as the path so that the contents of "another_picture.jpg" from my "~/Desktop" directory are base64 encoded for further processing and eventual upload.
Any and all help is much appreciated.
Edit 1:
My script where all the code is contained, "listener.py", is located in /root/PycharmProjects/virus_related/reverse_backdoor/. within this directory is a file that for simplicity's sake we can call "picture.jpg" The same file, "picture.jpg" is also located on my desktop, absolute path = "/root/Desktop/picture.jpg"
When I try read_file("picture.jpg"), there are no problems, the file is read.
When I try read_file("/root/Desktop/picture.jpg"), the file is not read and my terminal becomes stuck.
Edit 2:
I forgot to note that I am using the latest version of Kali Linux and Pycharm.
I have run "realpath picture.jpg" and it has yielded the path "/root/Desktop/picture.jpg"
Upon running read_file("/root/Desktop/picture.jpg"), I encounter the same problem where my terminal becomes stuck.
[FINAL EDIT aka Problem solved]:
Based on the answer suggesting trying to read a file like "../file", I realized that the code was fully functional because read_file("../file") worked without any flaws, indicating that my python script had no trouble locating the given path. Once the file was read, it was uploaded to the machine running my backdoor where, curiously, it uploaded the file to my target machine but in the parent directory of the script. It was then that I realized that problem lied in the handling of paths in the backdoor script rather than my listener.py
Credit is also due to the commentator who pointed out that "~" does not count as a valid path element. Once I reached the conclusion mentioned just above, I attempted read_file("~/Desktop/picture.jpg") which failed. But with a quick modification, read_file("/root/Desktop/picture.jpg") was successfully executed and the file was uploaded in the same directory as my backdoor script on my target machine once I implemented some quick-fix code.
My apologies for not being so specific; efforts to aid were certainly confounded by the unmentioned complexity of my situation and I would like to personally thank everyone who chipped in.
This was my first whole-hearted attempt to reach out to the stackoverflow community for help and I have not been disappointed. Cheers!
A solution I found is putting "../" before the filename if the path is right outside of the dictionary.
test.py (in some dictionary right inside dictionary "Desktop" (i.e. /Desktop/test):
with open("../test.txt", "r") as test:
print(test.readlines())
test.txt (in dictionary "/Desktop")
Hi!
Hello!
Result:
["Hi!", "Hello!"]
This is likely the simplest solution. I found this solution because I always use "cd ../" on the terminal.
This not only allows you to modify the current file, but all other files in the same directory as the one you are reading/writing to.
path = os.path.dirname(os.path.abspath(__file__))
dir_ = os.listdir(path)
for filename in dir_:
f = open(dir_ + '/' + filename)
content = f.read()
print filename, len(content)
try:
im = Image.open(filename)
im.show()
except IOError:
print('The following file is not an image type:', filename)
I am crawling through folders using the os.walk() method. In one of the folders, there is a large number of files, around 100,000 of them. The files look like: p_123_456.zip. But they are read as p123456.zip. Indeed, when I open windows explorer to browse the folder, for the first several seconds the files look like p123456.zip, but then change their appearance to p_123_456.zip. This is a strange scenario.
Now, I can't use time.sleep() because all folders and and files are being read into python variables in the looping line. Here is a snippet of the code:
for root, dirs, files in os.walk(srcFolder):
os.chdir(root)
for file in files:
shutil.copy(file, storeFolder)
In the last line, I get a file not found exception, saying that the file p123456.zip does not exist. Has anyone run into this mysterious issue? Anyway to bypass this? What is the cause of this? Thank you.
You don't seem to be concatenating the actual folder name with the filenames. Try changing your code to:
for root, dirs, files in os.walk(srcFolder):
for file in files:
shutil.copy(os.path.join(root, file), storeFolder)
os.chdir should be avoided like the plague. For one thing - if the changes suceeeds, it won't be the directory from which you are running your os.walk anymore - and then, a second chdir on another folder will fail (either stop your porgram or change you to an unexpected folder).
Just add the folder name as prefixes, and don't try using chdir.
Moreover, as for the comment from ShadowRanger above, os.walk officially breaks if you chdir inside its iteration - https://docs.python.org/3/library/os.html#os.walk - that is likely the root of the problem you had.