小编hja*_*mes的帖子

如何在没有密码的情况下使用paramiko连接到远程服务器?

我用Python写一个脚本,就必须连接到remote_server与SSH和移动fileremote_serverhost_server.我需要在没有密码的情况下这样做,因为它需要适用于任何远程服务器和任何主机服务器用户.

我的代码:

#get IP and username for remote access
IP = input("Enter host_server IP: ").split()
username = input("Enter username: ").split()
#password = ???????

#create a file on host_server for file
file_a = open(date+"file.txt", "a") #ignore the date variable
file = str(date+"file.txt")

#move file to host_server
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(IP[0], username = user[0], password = password[0])
print "Connected to server."
transfer = ssh.open_sftp()
transfer.get("file.txt", file)
transfer.close()
print "Transfer completed."
Run Code Online (Sandbox Code Playgroud)

问题: …

python ssh

11
推荐指数
1
解决办法
2万
查看次数

打开不同目录python中的所有文件

我需要在不使用它的路径的同时打开来自不同目录的文件,同时保留在当前目录中.

当我执行以下代码时:

for file in os.listdir(sub_dir):
    f = open(file, "r")
    lines = f.readlines()
    for line in lines:
        line.replace("dst=", ", ")
        line.replace("proto=", ", ")
        line.replace("dpt=", ", ")
Run Code Online (Sandbox Code Playgroud)

我收到错误消息,FileNotFoundError: [Errno 2] No such file or directory:因为它在子目录中.

问题:我是否可以使用os命令找到并打开文件sub_dir

谢谢!我知道如果这是一个重复,我搜索,找不到一个,但可能已经错过了它.

python python-3.x

9
推荐指数
2
解决办法
4万
查看次数

如何在文本文件中搜索单词并用Python打印部分行?

我正在写一个Python脚本.我需要在文本文件中搜索单词,然后打印该行的一部分.我的问题是这个词在文本文件中不是完全匹配的.

例如,在下面的文本文件示例中,我正在搜索该单词"color=".

文本文件ex:

ip=10.1.1.1 color=red house=big
ip=10.1.1.2 color=green house=small
ip=10.1.1.3 animal = dog house=beach
ip=10.1.1.4 color=yellow house=motorhome
Run Code Online (Sandbox Code Playgroud)

如果找到它,它应该打印到新的文本文件"color=color",而不是整行.

结果文本文件ex:

color=red
color=green
color=yellow
Run Code Online (Sandbox Code Playgroud)

我的代码:

for line_1 in open(file_1):
    with open(line_1+'.txt', 'a') as my_file:
        for line_2 in open(file_2):
            line_2_split = line_2.split(' ')
            if "word" in line_2:
                if "word 2" in line_2:
                    for part in line_2:
                        line_part = line_2.split(): #AttributeError: 'list' object has no attribute 'split'
                        if "color=" in line_part():
                            print(line_part)
Run Code Online (Sandbox Code Playgroud)

我相信我需要使用正则表达式或类似的东西line.find("color="),但我不确定是什么或如何.

问题:如何在文本文件中搜索单词(不完全匹配)并且只打印每行的特定部分?

python python-3.x

3
推荐指数
1
解决办法
5万
查看次数

在子目录Python中创建文件?

在我的Python脚本中,我需要在子目录中创建一个新文件而不更改目录,我需要从当前目录不断编辑该文件.

我的代码:

os.mkdir(datetime+"-dst")

for ip in open("list.txt"):
    with open(ip.strip()+".txt", "a") as ip_file: #this file needs to be created in the new directory
        for line in open("data.txt"):
            new_line = line.split(" ")
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    #write columns to new text file
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    try:
                        ip_file.write(", " + new_line[14] + "\n")
                    except IndexError:
                        pass
Run Code Online (Sandbox Code Playgroud)

问题:

目录和文件的路径并不总是相同,具体取决于我运行脚本的服务器.目录名称的一部分将是创建它的日期时间,即time.strftime("%y%m%d%H%M%S") + "word"如果时间不断变化,我不知道如何调用该目录.我以为我可以用shutil.move()它来创建文件后移动文件,但日期时间戳似乎有问题.

我是初学程序员,老实说我不知道​​如何处理这些问题.我在考虑将变量分配给目录和文件,但是日期时间让我感到沮丧.

问题:如果文件和子目录的名称/路径不总是相同,如何在子目录中创建文件?

python python-3.x

3
推荐指数
1
解决办法
2万
查看次数

从文本文件中提取列 - Python

我是初学者.

每次awk '{print $12}'在Python脚本中的文本文件(类似于Bash)中找到某个单词时,我需要从一个巨大的平面文本文件中提取第12列的内容.

到目前为止我有(例如,不是真正的代码):

word = a_word
for a_word in data:
   column12 = data.split()
   print(column12[11])
Run Code Online (Sandbox Code Playgroud)

我认为我应该从0开始计数而不是1,尽管我可能不正确.

此外,for这种代码的循环是否正确?

谢谢!

python

1
推荐指数
1
解决办法
2万
查看次数

在for循环Python中打开目录

我是初学程序员.

在我的Py脚本中,我需要创建一个CSV文件,其中包含子目录中多个文件的信息.当我运行我的代码时,我得到了错误:FileNotFoundError: [Errno 2] No such file or directory: '1'.我打印了目录的内容,发现该目录没有读取文件的实际名称,而是用数字或字符替换它.

问题:那是典型的目录吗?因为在研究中,似乎其他人都能够使用

for file in directory:
    open(file, "r")
Run Code Online (Sandbox Code Playgroud)

正确.我错过了什么吗?

我的代码:

sub_dir = datetime + "-dir"
os.mkdir(sub_dir)

with open("csv.txt", "a") as csv, open("data.txt") as data:
    csv.write("Title" + "\r")
    for x in data:
        csv.write("," + x)
    csv.write("\r" + "Data")                
    for file in sub_dir:
        csv.write(file)
        csv.write(", " + "Word1")
        csv.write(", " + "Word2")
        csv.write(", " + "Word3")
        csv.write(", " + "Word4")
        csv.write("\r")
        f = open(file, "r")
        lines = f.readlines()
        for line …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
1117
查看次数

标签 统计

python ×6

python-3.x ×3

ssh ×1