简体   繁体   English

让Python脚本在单独的目录中查找文件

[英]Have Python Script Locate Files in Seperate Directories

I'm pretty new to python programming, and I wrote a script to automate uploading a file via SFTP to a remote machine. 我是python编程的新手,我编写了一个脚本来自动通过SFTP将文件上传到远程计算机。 The script works wonderfully, but there is an issue that I can't seem to figure out. 该脚本运行出色,但是有一个我似乎无法弄清楚的问题。 If I'm in the directory in which the file I'm trying to upload is residing, everything goes fine. 如果我位于要上传的文件所在的目录中,则一切正常。 But, when I type the filename that is not residing in said directory, it doesn't like that. 但是,当我键入不在该目录中的文件名时,它不会那样。 It's a hassle having to browse to different folders each time. 每次必须浏览到不同的文件夹很麻烦。 I know I can consolidate the files into one folder... But I would love to try and automate this. 我知道我可以将文件合并到一个文件夹中...但是我很想尝试使它自动化。

The /Downloads directory is hard-coded since that's where most tools reside, does anyone know how I can tweak this line of code to grab the matching file name regardless of the directory the file resides in? /Downloads目录是硬编码的,因为这是大多数工具所在的目录,有人知道我可以如何调整此代码行以获取匹配的文件名,而不管文件所在的目录?

This is what I've written: 这是我写的:

#! /usr/bin/python2

# includes

import thirdpartylib
import sys

if len(sys.argv) != 6:
    print "Usage: %s file url port username password" % sys.argv[0]
    exit(0)
file = sys.argv[1]
host = sys.argv[2]
port = int(sys.argv[3])
username = sys.argv[4]
password = sys.argv[5]
filelocation = "Downloads/%s" % file

transport = thirdpartylib.Transport((host, port))
transport.connect(username=username, password=password)

sftp = thirdpartylib.SFTPClient.from_transport(transport)

sftp.put(file, filelocation)

sftp.close()
transport.close()

First of all, if you're doing any work with filepaths it is recommended that you use some built-in functionality to construct them to ensure that you have proper file separators etc. os.path.join is great for this. 首先,如果您正在使用文件路径进行任何工作,建议您使用一些内置功能来构造它们,以确保您具有适当的文件分隔符等os.path.join非常适合此功能。

That being said, I would recommend having the user pass in the file path as either an absolute path (in which case it can live anywhere on the machine) or a relative path (in which case it is relative to the current directory). 话虽如此,我建议用户将文件路径作为绝对路径(在这种情况下它可以存在于计算机上的任何位置)或相对路径(在这种情况下它相对于当前目录)进行传递。 I would not append Downloads/ to all the file paths as that obviously breaks any absolute paths and it requires the individual calling your program to know the internals of it. 不会Downloads/附加到所有文件路径,因为这显然会破坏任何绝对路径,并且需要调用程序的人员知道它的内部。 I think of this as the file path equivalent of a magic number . 我认为这是等效于魔术数字的文件路径。

So what that boils down to changing the filelocation to simply be the file input argument itself. 那么是什么归结为改变filelocation简单地在文件输入参数本身。

filelocation = sys.argv[1]

# You can even do some validation if you want
is not os.path.isfile(filelocation):
    print "File '%s' does not exist!" % filelocation

If you really want that Downloads/ folder to be the default (if the file path isn't an absolute path), you can check to see if the input is an absolute path (using os.path.isabs ) and if it's not, then specify that it's in the Downloads/ directory. 如果您确实希望将Downloads/文件夹作为默认路径(如果文件路径不是绝对路径),则可以检查输入是否是绝对路径(使用os.path.isabs ),如果不是,然后指定它在Downloads/目录中。

if not os.path.isabs(filelocation):
    filelocation = os.path.join('Downloads', filelocation)

Then users could call your script in two ways: 然后,用户可以通过两种方式调用您的脚本:

# Loads file in /absolute/path/to/file
./script.py /absolute/path/to/file ...

# Loads filename.txt from Downloads/filename.txt
./script.py filename.txt ...

Also, it looks like you may have your sftp.put input arguments reversed. 另外,看起来您的sftp.put输入参数可能已反转。 The local filename should come first. 本地文件名应该放在第一位。

I think you want filelocation as the first argument to stfp.put , as that is supposed to be the filename on the local machine. 我认为您希望将filelocation作为stfp.put的第一个参数,因为它应该是本地计算机上的文件名。 Also, you probably want to put a slash in front of Downloads . 另外,您可能想在Downloads前面加一个斜线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dockerfile 无法定位文件和目录 - Dockerfile unable to locate files and directories 将nzb文件下载到单独目录的良好后端 - Good backend for downloading nzb files to seperate directories 使用python脚本将文件分离到文件夹和子文件夹 - Seperate files to folders and sub folders using python script 在多个目录中的文件上以及需要提取的文件上运行python脚本 - Running a python script on files in multiple directories, on files that need to be extracted 在不再运行的容器中找到由 python 脚本编写的文件 - Locate files written by a python script in a container that is not running anymore Python脚本用于移动目录/文件,同时使用shutil忽略某些目录/文件? - Python script for moving directories/files while ignoring certain directories/files using shutil? Python脚本检查目录中的文件,然后通过电子邮件将名称发送给我 - Python script to check for files in directories then email me the names Boto3-查看所有目录和文件的python脚本 - Boto3 - python script to view all directories and files Python 2.7 脚本 - 在目录和子目录中的所有文件中搜索字符串 - Python 2.7 Script - Searching for a String in all files in Directories and Sub-Directories 使用 Python 的文件和目录 - Files and Directories using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM