Trying to run bot in server. Ubuntu 20.1
Line in code(Python3)
...
for elem in items['stats']:
f = open(r"\root\bot_python\list.txt", "r+")
...
Traceback
Traceback (most recent call last):
File "bot_last_final_18_01_2021.py", line 57, in <module>
f = open(r"\root\bot_python\list.txt", "r+")
FileNotFoundError: [Errno 2] No such file or directory: '\\root\\bot_python\\list.txt'
How correctly write directoty?
Directory paths in Linux use forward slash, so try:
for elem in items['stats']:
f = open("/root/bot_python/list.txt", "r+")
On linux you use "/" instead of "\". So the file location would be /root/bot_python/list.txt.
If that doesn't work then try without the .txt at the end. That should be it
Related
I have looked at How to open a list of files in Python This problem similar but not covered.
path = "C:\\test\\test5\\"
files = os.listdir(path)
fileNames = []
for f in files:
fileNames.append(f)
for fileName in fileNames:
pathFileName = path + fileName
print(f"This is the path: {pathFileName}")
fin = open(pathFileName, 'rt')
texts = []
with open(fileName) as file_in:
# read file text lines into an array
for text in file_in:
texts.append(text)
for text in texts:
print(text)
The file aaaatest.txt is in C:\test\test5 The output is:
This is the path: C:\test\test5\aaaatest.txt
Traceback (most recent call last):
File "c:\Users\david\source\repos\python-street-spell\diffLibFieldFix.py", line 30, in <module>
with open(fileName) as file_in:
FileNotFoundError: [Errno 2] No such file or directory: 'aaaatest.txt'
So here's the point. If I take a copy of aaaatest.txt (leaving original where it is) and put it in the current working directory. Running the script again I get:
This is the path: C:\test\test5\aaaatest.txt
A triple AAA test
This is the path: C:\test\test5\AALTONEN-ALLAN_PENCARROW_PAGE_1.txt
Traceback (most recent call last):
File "c:\Users\david\source\repos\python-street-spell\diffLibFieldFix.py", line 30, in <module>
with open(fileName) as file_in:
FileNotFoundError: [Errno 2] No such file or directory: 'AALTONEN-ALLAN_PENCARROW_PAGE_1.txt'
The file aaaatest.txt is opened and the single line of text, contained in it, is outputted. Following this an attempt is made to open the next file of C:\test\test5 where the same error occurs again.
Seems to me that while the path is saying C:\test\test5 the file is only being read from the cwd?
I have written this Python script that checks if a file exists in a web server. And if it does exist, it copies is in the "found" directory on the local machine. However, since the web server is running Linux, and the full file path has slashes in it, it shows some errors. I have tried to solve it with "os.path" module but have been unsuccessful in my attempts.
import requests
import sys
import re
regex = re.compile(r"header(.*) footer", re.DOTALL)
data = {
"name":"user01",
"password":"secret"
}
r = requests.post('http://10.10.10.10/posts', data=data)
file = re.search(regex, r.text)
filename = sys.argv[1]
if file.group(1) != 'None':
with open('found/' + filename, 'w') as f:
f.write(file.group(1))
print("Found: ", filename)
I get the following error:
ERROR
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//boot/grub/grub.cfg'
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/adduser.conf'
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/apache2/apache2.conf'
I have come up with a workaround. All I have to do is use:
filename = sys.argv[1].replace("/", "_")
It saves the files with an "_" instead of "/". For example, it saves "/etc/passwd" as "_etc_passwd".
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 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+')
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')
...
>>>