Issue with File Not Found - python

I'm having an issue just drawing from a file which I don't think I've had an issue with before. I'm not sure if it's because I switched from PyCharm to IDLE
Here is my current code:
import time
import os
keep_running = True
last_time = 0
file = os.path.abspath(r'C:\Users\AUser\Desktop\test.txt')
current_time = os.path.getmtime(file)
print(file)
Here is the output I'm getting is:
Traceback (most recent call last):
File "C:/Users/AUser/Desktop/Scripts/FileAlert.py", line 9, in <module>
current_time = os.path.getmtime(file)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\AUser\\Desktop\\test.txt'
If I remove 'r' from the file path, I get a unicode error instead. The file is in a different directory from the script so I'm not sure what the issue is. This is happening with Python 3.9 on a Windows 10 machine.

In this case the issue is the file being searched for was technically named 'test.txt.txt'.
Using this suggestion helped me to locate this error on my end:
Does that file exist? It seems it does not. You could try import os and then os.listdir('C:\Users\AUser\Desktop') to get the directory list. from user #tdelaney

Related

reading pg_dump file occurs at open the file

I'm using the pgdumplib lib. Unfortunately there is an error, when I'm trying to open the file. The file is in the same folder as the python script. I'm using Python 3.7
Code:
import pgdumplib
dump = pgdumplib.load('test.dump')
print('Database: {}'.format(dump.toc.dbname))
print('Archive Timestamp: {}'.format(dump.toc.timestamp))
print('Server Version: {}'.format(dump.toc.server_version))
print('Dump Version: {}'.format(dump.toc.dump_version))
for line in dump.table_data('public', 'pgbench_accounts'):
print(line)
Error:
Traceback (most recent call last):
File "C:/Users/user/data/test.py", line 3, in <module>
dump = pgdumplib.load('test.dump')
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\__init__.py", line 24, in load
return dump.Dump(converter=converter).load(filepath)
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\dump.py", line 228, in load
raise ValueError('Path {!r} does not exist'.format(path))
ValueError: Path 'test.dump' does not exist
If you are running your code from C:/Users/user/700Joach/project/ and you have the following line in your script:
dump = pgdumplib.load('test.dump')
Then, python would look for the following path to open test.dump:
C:/Users/user/700Joach/project/test.dump
Namely, this part: load('test.dump') internally is forging a relative path to test.dump.
You can do several things to resolve the issue. Either move test.dump to the directory from which you are executing your code. Or, provide an absolute path to your test.dump as follows:
dump = pgdumplib.load('C:/Users/user/700Joach/project/test.dump')

gensim file not found error

I am executing the following line:
id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
This code is available at "https://radimrehurek.com/gensim/wiki.html". I downloaded the wikipedia corpus and generated the required files and wiki_en_wordids.txt is one of those files. This file is available in the following location:
~/gensim/results/wiki_en
So when i execute the code mentioned above I get the following error:
Traceback (most recent call last):
File "~\Python\Python36-32\temp.py", line 5, in <module>
id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
File "~\Python\Python36-32\lib\site-packages\gensim\corpora\dictionary.py", line 344, in load_from_text
with utils.smart_open(fname) as f:
File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 129, in smart_open
return file_smart_open(parsed_uri.uri_path, mode)
File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 613, in file_smart_open
return open(fname, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'wiki_en_wordids.txt'
Even though the file is available in the required location I get that error. Should I place the file in any other location? How do I determine what the right location is?
The code requires an absolute path here. Relative path should be used when entire operation is carried out in the same directory location, but in this case, the file name is passed as argument to some other function which is located at different location.
One way to handle this situation is using abspath -
import os
id2word = gensim.corpora.Dictionary.load_from_text(os.path.abspath('wiki_en_wordids.txt'))

Error when moving files

I'm trying to write a code that moves files in my download folder to other specified folders but I keep getting errors. Here's my code.
import os
import shutil
series = []
for i in os.listdir('C:\\Users\\Mike\\Downloads\\Video'):
if ('.mp4') in i:
series.append(i)
for j in series:
if 'Thrones' in j:
shutil.move(j,'C:\\Users\\Mike\\Desktop\\')
I keep getting this error
Traceback (most recent call last):
File "C:/Users/Mike/Downloads/Video/Arrange.py", line 70, in <module>
Series(series)
File "C:/Users/Mike/Downloads/Video/Arrange.py", line 48, in Series
shutil.move(serie, 'C:\\Users\\Mike\\Desktop\\Movies\\Series\\Lost\\s2\\')
File "C:\Users\Mike\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 536, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 'C:\Users\Mike\Desktop\Movies\Series\Lost\s2\lost - s02e08 (o2tvseries.com).mp4' already exists
>>>
but the file actually moves. How do I move the files without getting this error each time?
The error you get is Windows platform specific. You are using shutil.move which uses os.rename under the hood. From docs:
On Windows, if dst already exists, OSError will be raised even if it is a file
You could check if the file exists in the destination before you move it and depending what you want to achieve:
1) don't overwrite the destination, just remove file from source
2) remove the file from source first and overwrite the destination
Below you can find implementation of solution 2)
import os
for name in series:
if 'Thrones' in name:
if not os.path.isfile(name):
shutil.move(name, 'C:\\Users\\Mike\\Desktop\\')
else:
os.remove(name)

Python os.walk() failing

I have created a script to give me the list of files in a folder directory. Yet, I am occasionally getting this error. What does this mean?
portion of the error:
Script failed due to an error:
Traceback (most recent call last):
File "<script>", line 12, in <module>
File "C:\Program Files\Nuix\Nuix 6\lib\jython.jar\Lib\os.py", line 309, in walk
File "C:\Program Files\Nuix\Nuix 6\lib\jython.jar\Lib\os.py", line 299, in walk
File "C:\Program Files\Nuix\Nuix 6\lib\jython.jar\Lib\genericpath.py", line 41, in isdir
File "C:\Program Files\Nuix\Nuix 6\lib\jython.jar\Lib\genericpath.py", line 41, in isdir
java.lang.AbstractMethodError: org.python.modules.posix.PythonPOSIXHandler.error(Ljnr/constants/platform/Errno;Ljava/lang/String;Ljava/lang/String;)V
at jnr.posix.BaseNativePOSIX.stat(BaseNativePOSIX.java:309)
at jnr.posix.CheckedPOSIX.stat(CheckedPOSIX.java:265)
at jnr.posix.LazyPOSIX.stat(LazyPOSIX.java:267)
The script:
import os
import codecs
import shutil
import datetime
import sys
exportpath = 'P:/Output/Export7/{6136BAF2-85BA-4E64-8C11-A2C59398FC02}/'
tempnativefolder = 'NATIVESOrig'
for dir, sub, file in os.walk(exportpath + tempnativefolder):
for fname in file:
#source path
source = os.path.join(dir, fname).replace('\\', '/')
print source
print("Natives moved to subfolders")
I found out that the presence of these characters(see "diamond with question mark" character in screenshot) in the file name causes the issue. Once I replaced those, my script works. thanks so much.
What the error means: AbstractMethodError means that some code tried to call a method which was not implemented.
PythonPOSIXHandler implements jnr.posix.POSIXHandler. JRuby also uses JNR and the interface is subtly different between the two. JRuby's newer copy of JNR has that one additional #error(Errno, String, String) method and Jython's implementation lacks that method, because it's compiled against the interface when the method didn't exist.
I usually see this problem in the other direction - where stuff in Jython's jar breaks JRuby. I assume it entirely depends on the order of the jars in the classpath.

Script not working since windows 7

I have the following script which worked fine on XP, since I have a new PC on Windows 7 Professional the code has stopped working
import os
import shutil
from time import strftime
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
zip_program="zip.exe"
for files in os.listdir(logsdir):
if files.endswith(".log"):
files1=files+"."+strftime("%Y-%m-%d")+".zip"
os.chdir(logsdir)
os.system(zip_program + " " + files1 +" "+ files)
shutil.move(files1, zipdir)
os.remove(files)
The error I am getting is
U:>python logs.py
zip warning: name not matched: ping_dms_155.log
zip error: Nothing to do! (ping_dms_155.log.2013-05-14.zip)
Traceback (most recent call last):
File "logs.py", line 24, in <module>
shutil.move(files1, zipdir)
File "c:\python27\lib\shutil.py", line 301, in move
copy2(src, real_dst)
File "c:\python27\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "c:\python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'ping_dms_155.log.2013-05-14.zip'
I can't think why it would stop working, thanks in advance
It seems you do have zip.exe on your windows 7 machine from the error, but it may not be a version compatible with Windows 7.
Check in logsdir to see if the file you modify (ping_dms_155.log.2013-05-14.zip) already exists. If all of these are true I would suggest using python module zipfile.
Your path strings aren't escaped properly.
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
should be:
logsdir=r"c:\logs"
zipdir=r"c:\logs\puttylogs\zipped_logs"
The directory c:ogs doesn't exist. Running it manually worked because you changed to the log directory. It ran on XP because... well, you didn't run this exact script on XP because it wouldn't work there either.
I have got this to work by changing the os.system to subprocess so the code now looks like
import os
import shutil
from time import strftime
import subprocess
logsdir="c:\logs"
zipdir="c:\logs\puttylogs\zipped_logs"
zip_program="zip.exe"
for files in os.listdir(logsdir):
if files.endswith(".log"):
files1=files+"."+strftime("%Y%m%d")+".zip"
os.chdir(logsdir)
subprocess.call([zip_program,files1, files])
shutil.move(files1, zipdir)
os.remove(files)

Categories