简体   繁体   English

如何在Python中正确更改文件路径的名称?

[英]How do I change the name of a file path correctly in Python?

My code 我的密码

specFileName = input("Enter the file path of the program you would like to capslock: ")



inFile = open(specFileName, 'r')
ified = inFile.read().upper()

outFile = open(specFileName + "UPPER", 'w')
outFile.write(ified)
outFile.close()


print(inFile.read())

This is basically make to take in any file, capitalize everything, and put it into a new file called UPPER"filename". 基本上,这是使任何文件都大写,然后将其放入名为UPPER“文件名”的新文件中。 How do I add the "UPPER" bit into the variable without it being at the very end or very beginning? 如何将“ UPPER”位添加到变量中,而不必在结尾或开头呢? As it won't work like that due to the rest of the file path in the beginning and the file extension at the end. 由于开头的文件路径的其余部分和结尾的文件扩展名,因此无法正常工作。 For example, C:/users/me/directory/file.txt would become C:/users/me/directory/UPPERfile.txt 例如,C:/users/me/directory/file.txt将变为C:/users/me/directory/UPPERfile.txt

Look into the methods os.path.split and os.path.splitext from the os.path module. os.path模块中查看方法os.path.splitos.path.splitext

Also, quick reminder: don't forget to close your "infile". 另外,快速提醒您:不要忘记关闭您的“ infile”。

Depending on exactly how you're trying to do this, there's several approaches. 根据您尝试执行此操作的确切方式,有几种方法。

First of all you probably want to grab just the filename, not the whole path. 首先,您可能只想获取文件名,而不是整个路径。 Do this with os.path.split . 使用os.path.split做到这一点。

>>> pathname = r"C:\windows\system32\test.txt"
>>> os.path.split(pathname)
('C:\\windows\\system32', 'test.txt')

Then you can also look at os.path.splitext 然后,您也可以查看os.path.splitext

>>> filename = "test.old.txt"
>>> os.path.splitext(filename)
('test.old', '.txt')

And finally string formatting would be good 最后,字符串格式化会很好

>>> test_string = "Hello, {}"
>>> test_string.format("world") + ".txt"
"Hello, world.txt"

Put 'em together and you've probably got something like: 将它们放在一起,您可能会得到类似以下内容的信息:

def make_upper(filename, new_filename):
    with open(filename) as infile:
        data = infile.read()
    with open(new_filename) as outfile:
        outfile.write(data.upper())

def main():
    user_in = input("What's the path to your file? ")
    path = user_in # just for clarity
    root, filename = os.path.split(user_in)
    head,tail = os.path.splitext(filename)
    new_filename = "UPPER{}{}".format(head,tail)
    new_path = os.path.join(root, new_filename)
    make_upper(path, new_path)

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

相关问题 如何在Python中更改/选择文件路径? - How do I change/choose the file path in Python? 如何在python中自动更改包含日期的文件名 - How do I change file name that contains a date automatically in python 当文件名是 python 中的变量时,如何更改文件名的一部分? - How do I change part of a file name when it is a variable in python? 如何在bash或python中将文件路径中的部分目录名称附加到文件名称中? - How do I append parts of the directory names in a file's path to the file's name in bash or python? 在 Python 中,如何获取文件的正确大小写路径? - In Python, how can I get the correctly-cased path for a file? 如何在python中传递文件路径? - How do I pass a file path in python? 如何根据Python中文件名中的数字更改文件扩展名 - How do I change file extension based on the number in a file name in Python Maya Python:如何获取场景名称但不获取路径或扩展名 - Maya Python: How do I get the scene name but not the path or extension 在python中,如何获得“。”的完整路径名? - In python, how do I get the full path name of “.”? 如何正确应用 RE 以从给定路径获取姓氏(文件或文件夹的)并将其打印在 Python 上? - How to correctly apply a RE for obtaining the last name (of a file or folder) from a given path and print it on Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM