FTP giving 550 error when using ftp.cwd() twice - python

When I run this part:
directory = ftp.pwd()
file_name = 'config.single'
ftp.cwd('plugins/GAListener')
print('dir:', directory)
ftp.cwd('plugins/CrateReloaded/crates')
It says:
ftplib.error_perm: 550 No such file or directory.
When I change the directory several times inside an if statement, it works fine.
Are you unable to change the working directory like that? Do I need to reset to the main server directory prior to changing to another? If so, how would I do that?
Whichever directory comes first in the code is the one it goes to, then gives an error upon trying to change to the second. With the second ftp.cwd() commented out, the first one runs without problem, no matter which directory it points to, so they're definitely both there.
Additionally, when I print directory, it just prints / and not plugins/GAListener.
Edit: When doing this inside an if statement, all the directories go where they're supposed to and I'm given no errors without a leading slash.
if day_of_week == 0 and file_name not in ftp.nlst():
ftp.rename('config.yml', 'config.single')
ftp.rename('config.double', 'config.yml')
print('plugins/GAListener/config.yml is now plugins/GAListener/config.single.')
print('plugins/GAListener/config.double is now plugins/GAListener/config.yml.')
ftp.cwd('plugins/MOTDCountdown')
ftp.rename('config.yml', 'config.sunday')
ftp.rename('config.monday', 'config.yml')
print('plugins/MOTDCountdown/config.yml is now plugins/MOTDCountdown/config.sunday.')
print('plugins/MOTDCountdown/config.monday is now plugins/MOTDCountdown/config.yml.')
ftp.cwd('plugins/Essentials')
ftp.rename('motd.txt', 'motd.sunday')
ftp.rename('motd.monday', 'motd.txt')

After the first cwd you end up in folder:
/plugins/GAListener
Changing to a relative path plugins/CrateReloaded/crates (without a leading slash) will resolve it against the current working directory. So it will attempt to open folder:
/plugins/GAListener/plugins/CrateReloaded/crates
Which most probably does not exist.
I assume you want to go to
/plugins/CrateReloaded/crates
For that you have to use an absolute path (with a leading slash):
ftp.cwd('/plugins/CrateReloaded/crates')

Related

Python script execution directory changes when I execute outside of Console

TLDR: I want to change the working directory of my script to the script file's location(instead of system32) when i run it by double clicking on it.
I have a really annoying problem I couldn't solve. I am building a python script that will take 4 text files as input and create some graphs and an excel sheet using these text files. I am going to pass my script to a friend who will copy this script into different folders and execute the script in those folders by just double-clicking on the script. The problem I am facing is when I execute my code out of cmd everything works fine. But if I double click on it, the directory my code is working changes automatically, and my program can't find the required 4 text files. I am attaching the required parts of my code below and also attaching some screenshots.
ss1
ss2
def fileOpenCheckLoad(fname):
pl=list()
try:
fh=open(fname)
except:
print("ERROR:"+ fname +" is missing. Execution will terminate.")
x=input("Press enter to quit.")
quit()
test1=fh.readline()
test2=fh.readline()
if test1[6]!=fname[5] and test2!='t x y\n' :
print("ERROR: Check the contents of:"+ fname)
x=input("Press enter to quit.")
quit()
count=0
for lines in fh:
count=count+1
if count>2 :
nums=lines.split()
pl.append((float(nums[2]), float(nums[2])))
tbr=(pl,count-2)
return tbr
# main starts here.
cwd = os.getcwd()
print(cwd)
# In this part we open and load files into the memory. If there is an error we terminate.
(pl1, count1)=fileOpenCheckLoad('point1.txt')
(pl2, count2)=fileOpenCheckLoad('point2.txt')
(pl3, count3)=fileOpenCheckLoad('point3.txt')
(pl4, count4)=fileOpenCheckLoad('point4.txt')
Before calling os.getcwd(), insert this line:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Explanation
__file__ is a special variable in Python; as described here, "__file__ is the pathname of the file from which the module was loaded"
os.path.abspath returns the absolute path to the input file or directory (I included this because depending on how you load up a Python file, sometimes __file__ will be stored as a relative path, and absolute paths tend to be safer to work with)
os.path.dirname returns the name of the directory which contains the input file (because chdir will return an error if we give it the name of a file, so we need to give it the name of the directory which contains the file)
os.chdir changes the working directory

Why does a python program continue to write to the directory in which it was originally located, after I move the file to another directory?

I have 2 directories. I had a python program located in dir_1 writing to a .txt file also in dir_1. I meant to create them in dir_2, but when I move them both to dir_2, the python program, instead of writing to the existing .txt file that is now with it in dir_2, creates a new .txt file in dir_1 and writes to it. How do I fix this? I'm very new to programming and python and googling didn't help me out, probably because I didn't know what exactly to search.
with open('guest_book.txt', 'w') as file:
while True:
name = input('Please enter your name: ')
if name == 'q':
break
else:
print(f"Hello, {name.title()}!\nYou have been added to the guest"
f"book")
file.write(f"{name.title()}\n")
Python writes to the file location you supply it with. If this file location is a relative path, then it will create files relative to the directory of the script. I.e. when you move the script then the .txt file will be created relative to the new directiory.
On the other hand, if you provide an absolute path, then it does not matter where the script is located / where you execute it from. Instead, it will create the file at that location always.
From the sounds of it, you are using an absolute path when you want a relative path.
So change from something like /home/bob/file.txt (Linux) or C:\\Users\Bob\file.txt (Win) to simply file.txt or even ./file.txt.
Update: Since you were using a relative location all along, the problem will lie with the context that you are executing the script from. Your code is not the issue here, it is how you are executing it.
As vlovero suggests, maybe your IDE is not executing the new file in its new location?
One way you can test this robustly is to navigate to dir_2 in a terminal and run
python your_program_name.py
This will execute the script in the dir_2 location.
Since you have not specified an absolute path, your program is then specifying a directory relative to the current working directory (if instead, for example, you had specified a path such as '../guest_book.txt', you would have been specifying a directory one level above the current working directory). So let's imagine your OS is Linux and the Python program resides in /my_home/programs:
cd /my_home/data # this is the current working directory
python ../programs/your_program.py
The current working directory when the program is executed is /home/my_home/data even though the program being executed resides in /my_home/programs, and thus the output file will be created in the /my_home/data directory. os.getcwd() can be called to tell you what the current working directory is.

What directory does os.path.join start at?

I made a script in the past to mass rename any file greater than x characters in a directory. When I made that script I had a source directory which you would need to input manually. Any file that was over x characters in that directory would be stripped of it's extension, renamed, then the extension would be re added and it would use os.path.join to join the source and the newly created filename+ext. I'm now making another script and used os.path.join("Folder in the current dir", "file in that dir"). Because this worked I'm guessing that when os.path.join is called with just a foldername and no full path in it's first parameter it starts it's search from the directory that the script it was run in? Just wondering if this is correct.
os.path.join has nothing to do with any actual filesystem, and does not "start" anywhere. It simply joins two arbitrary paths, whether they exist or not.
What os.path.join does is to just join path elements the system-compatible way, taking into effect the particular directory separator character, etc., into account. It's a simple string manipulation tool.
So the returned result simply starts from whatever you give to it as the first argument.

Cannot access file on pythonanywhere

I have a django project that worked perfectly on my local server returning a response. I am now trying to run it on pythonanywhere, it keeps saying no such directory or file. I initially used the os.path.dirname("__file__") but then I changed it into the absolute address, i.e. "/home/username/projectname/filename" to no avail. That latter method is the only one others on the web are suggesting, but it still isn't working. Is there a special syntax to access files in pythonanywhere? or do you have any suggestions? Thanks.
The following is the line that throws the error:
with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file:
Directory structure:
If this with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file:
is the actual code you're using, then you're missing a / at the beginning. What you're actually asking for with that code is not the absolute path to layer.pem, but a relative path rooted in the current directory.
Also, the os.path.dirname("__file__") is not working because you quoted the __file__. What you're asking for is the dirname of a file called "__file__" (which will be an empty string), not the dirname of the current file.

Change to a known directory name but unkown absolute path in Python

I would like to change the cwd to a specific folder.
The folder name is known; however, the path to it will vary.
I am attempting the following but cannot seem to get what I am looking for:
absolute_path = os.path.abspath(folder_name)
directory_path = os.path.dirname(absolute_path)
os.chdir(directory_path)
This does not do what I'm looking for because it is keeping the original cwd to where the .py file is run from. I've tried adding os.chdir(os.path.expanduser("~")) prior to the first code block; however, it just creates the absolute_path to /home/user/folder_name.
Of course if there is a simple import that I could use, I'll be open to anything.
What would be the correct way to get the paths of all folders with with a specific name?
def find_folders(start_path,needle):
for cwd, folders, files,in os.walk(start_path):
if needle in folders:
yield os.path.join(cwd,needle)
for path in find_folders("/","a_folder_named_x"):
print path
all this is doing is walking down your directory structure from a given start path and finding all occurances of a folder named needle
in the example it is starting at the root folder of the system and looking for a folder named "a_folder_named_x" ... be forwarned this could take a while to run if you need to search the whole system ...
You need to understand that abspath accepts a relative pathname (which might just be a filename), and gives you the equivalent absolute (full) pathname. A relative pathname is one that begins in your current directory; no searching is involved, and so it always points to one place (which may or may not exist).
What you actually need is to search down a directory tree, starting at ~ or whatever directory makes sense in your case, until you find a folder with the requested name. That's what #Joran's code does.

Categories

Resources