I write a basic file write code -
f = open('workfile', 'r+')
f.write('0123456789abcdef')
I run the file at cmd at the same folder where I put workfile.txt file but I get the error -
IOError: [Errno 2] No such file or directory: 'workfile'
I tried also to convert to exe file with py2exe and run the exe file but nothing...
what is the problem?
thanks!
====================
the full error massage when check on compiler-
Traceback (most recent call last):
File "/home/ubuntu/workspace/.c9/metadata/workspace/2.py", line 1, in <module>
f = open('workfile', 'r+')
IOError: [Errno 2] No such file or directory: 'workfile'
rename 'workfile' to 'workfile.txt':
f = open('workfile.txt', 'r+')
Related
#import necessary modules
import csv
with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
data = csv.reader(f)
for row in data:
print(row)
Heres the error message
Traceback (most recent call last):
File "/Users/noahhenninger/PycharmProjects/Refactoring/main.py", line 3, in <module>
with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/iCloud Drive/Desktop/Python/o.csv'
I tried adding more specific info about the location of the file but that didn't seem to work.
I am trying to unzip a number of files that are password protected but I keep getting some permission error. I have tried to perform this operation running vscode as an administrator but I am still getting the same error.
Here is the code:
input_file = ".\\pa-dirty-price-crawler\\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(input_file, pwd=b'qpsqpwsr')
Here is the error:
Traceback (most recent call last):
File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
with zipfile.ZipFile(input_file, 'r') as zip_ref:
File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: '.\\pa-dirty-price-crawler\\folders'
I do not know of another library that can do this same operation but if anyone has suggestions in regards to getting this fixed I would really appreciate it.
Edit:
When I try to specify the entire file path name as so:
input_file = "C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(pwd=b'qpsqpwsr')
I still get this error:
Traceback (most recent call last):
File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
with zipfile.ZipFile(input_file, 'r') as zip_ref:
File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders'
It looks like you're passing a directory as the input. This is the likely problem, not that the zip is password protected.
To extract a zip file, zipfile.ZipFile takes a zip file as an input not a directory.
Therefore, your code needs two variables: an input zip file and an output directory:
input_file = r".\pa-dirty-price-crawler\folders\myzipfile.zip"
output_directory = r".\pa-dirty-price-crawler\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(output_directory, pwd=b'qpsqpwsr')
* note the use of r"string", this helps having to escape all your back slashes
I am trying to create a text file, using open(filename,'x'). I have tried x = 'a+', 'w+', 'w'. I am using windows 10, vs code "run python file in terminal", and python 3.8.2
import os
print("cwd",os.getcwd())
scriptpath = os.path.dirname(__file__)
filename = "test.txt" #1
#filename = scriptpath + "/test.txt" #2
#filename = r"C:\Users\harki\Documents\ALGO\ALGO-NPL\test.txt" #3
f = open(filename,'w+')
f.write("test")
f.close()
running with first filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
running with second filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'c:/Users/harki/Documents/ALGO/ALGO-NPL/test.txt'
running with third filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\harki\\Documents\\ALGO\\ALGO-NPL\\test.txt'
Edit:
Moving project outside of "Documents" folder solved the problem
I used this code to define file path and it works for me:
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'folder/name', 'test.txt')
And then I create the file by using joblib or pickle.
In your case, it is similar to the second filename, just need to add the join() function
I'm trying to rename a set of files using the following code. The File is within the folder but it gives the following error. My code is attached here with:
import os
path='absolute_path'
arr = os.listdir(path)
for i in arr:
old_name=i
old_name_part=old_name.split(".")
new_name=old_name_part[0]+".png"
print(i,'\t',old_name,'\t',new_name)
os.rename(i,new_name)
Error :
drone_0002_01320.jpg drone_0002_01320.jpg drone_0002_01320.png
Traceback (most recent call last):
File "/absolute_path/rename.py", line 23, in <module>
os.rename(i,new_name)
FileNotFoundError: [Errno 2] No such file or directory: 'drone_0002_01320.jpg' -> 'drone_0002_01320.png'
os.rename(i,new_name) takes the full path of your file
you can change it into this:
os.rename(os.path.join(path, i),os.path.join(path, new_name))
Consider read about pathlib module, it has some good function for those kind of problems
I want to create and write a .txt file in a hidden folder using python. I am using this code:
file_name="hi.txt"
temp_path = '~/.myfolder/docs/' + file_name
file = open(temp_path, 'w')
file.write('editing the file')
file.close()
print 'Execution completed.'
where ~/.myfolder/docs/ is a hidden folder. I ma getting the error:
Traceback (most recent call last):
File "test.py", line 3, in <module>
file = open(temp_path, 'w')
IOError: [Errno 2] No such file or directory: '~/.myfolder/docs/hi.txt'
The same code works when I save the file in some non-hidden folder.
Any ideas why open() is not working for hidden folders.
The problem isn't that it's hidden, it's that Python cannot resolve your use of ~ representing your home directory. Use os.path.expanduser,
>>> with open('~/.FDSA', 'w') as f:
... f.write('hi')
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '~/.FDSA'
>>>
>>> import os
>>> with open(os.path.expanduser('~/.FDSA'), 'w') as f:
... f.write('hi')
...
>>>